Reading a name
name = input("What is your name? ") print("Namaskar,", name)
Uses the input Sourav
Write Python. Understand every error.
Python basics · topic 6 of 25
input() pauses the program and waits for the user to type something. Whatever they type is returned as a string. You can show a message, called a prompt, inside the brackets.
name = input("What is your name? ") print("Hello,", name)
Because input() always gives text, convert it before doing arithmetic: age = int(input()).
An f-string makes it easy to build a message from values. Put f before the quote, and write variables inside { }. You can also control decimals: {price:.2f} shows exactly two decimal places.
item = "tea" price = 12.5 print(f"One {item} costs {price:.2f} rupees")
In this app, each line of the Program input box answers one input() call. When your exercise is checked, only what you print() is compared; prompt messages are ignored.
input() waits for the user to type and always returns text, even when they type digits.age = int(input()).f"Hello {name}", and {price:.2f} shows exactly two decimals.input() call.input() values without converting them. "4" + "6" gives 46, not 10.int(input()). Read the ValueError guide.input() more times than there are lines of input. Read the EOFError guide.
name = input("What is your name? ") print("Namaskar,", name)
Uses the input Sourav
age = int(input("Your age: ")) print("Next year you will be", age + 1)
Uses the input 19
name = "Meera" marks = 91 print(f"{name} scored {marks} marks")
total = 245 / 7 print(total) print(f"{total:.2f}")
5 quick questions to check your understanding. Choose an answer for each, then press Check answers.
input() always return?input() always gives back text. Use int() or float() to convert it.
name = "Riya" print(f"Hello {name}!")
An f-string replaces {name} with the value of the variable.
x = 3.14159 print(f"{x:.2f}")
:.2f shows the number with 2 digits after the decimal point.
input() * 2 give?input() gives the string "5", and a string times 2 repeats it.
a = "4" b = "6" print(int(a) + int(b))
Both strings are converted to numbers first, so they are added.