Chapter 5 Flashcards
What functions can help the program to prompt us for information to make the program interactive?
input()
print()
Write the script for the result:
“Hello World, my name is Amrapali, and I am 33 years old”
myName= input”Please enter your name:”
myAge=input”What about your age:”
print(“Hello World, My name is”, myName, “And I am”, myAge, “years old”)
How can you edit the input string?
with the % formatter or format()
Briefly explain how the input function works.
When the print command is executed, the input function prompts the user for his/her name and then when it is entered it stores the name as a string in the variable myName. Also, it stores the age as a string in the variable myAge and later when that is entered it executes the script to display Hello World, my name is Amrapali and I am 33 years old.
Write the script for an interactive program using % formatter function to yield:
Hello World, My name is A and, I am 2 years old
userName=input(“What’s your name?:”)
userAge=input(“Hi %s, What’s your age?:”%(userName))
print(“Hello World, My name is”, userName,”,and I am”,userAge, “years old”)
Write the script for an interactive program using format() function to yield:
Hello World, My name is A and, I am 2 years old
userName=input(“What’s your name?:”)
userAge=input(“Hi {}, What’s your age?:”.format(userName))
print(“Hello World, My name is”, userName,”,and I am”,userAge, “years old”)
What is the print function?
It is used to display information to the users. It accepts zero or more expressions as arguments, separated by commas.
Identify arguments in the below print function
print(“Hello World, my name is”,myName,”and I am”,myAge,”years old”)
There are five arguments here:
- Hello World, my name is
- myName
- and I am
- myAge
- years old
Write the script for using the %formatter with the print function.
userName=”Achilles”
userAge=2
print(“Hello World, my name is %s and I am %d years old”%(userName,userAge)
Write the script using format()+print()
userName=”Achilles”
userAge=2
print(“Hello World, my name is {} and I am {} years old”.format(userName,userAge))
How print() function can be used to display a long message?
By using triple quotes.
print(“"”Hello World, My name is Calvin and I am 4 years old”””)
Why are triple quotes useful?
They improve the readability of the message.
Write the script for printing tabor space between two words
print(“Hello\tworld”)
Write the script for printing a new line.
> > > print(“Hello\nWorld”)
Write the script for printing “"
print(“\”)