Remember
input() displays an optional prompt and returns the entered line as a string. The response is supplied as a fixture in this guided simulation.
input() displays an optional prompt and returns the entered line as a string. The response is supplied as a fixture in this guided simulation.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
response = input("Name: ")
print("Hello,", response)Top-level scope
'Ada'input() displays an optional prompt and returns the entered line as a string. The response is supplied as a fixture in this guided simulation.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
response = input("Name: ")
print("Hello,", response)
|
v
DISPLAYED RESULT
Name: Hello, Ada
FINAL BINDINGS
response --> object 1 (str)
object 1: 'Ada'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
response = input("Name: ")
print("Hello,", response)Top-level scope
'Ada'The example has finished. Compare the final output with the values and references above.
Name: Hello, Ada
What do you expect this program to print?
response = input("Name: ")
print("Hello,", response)input() FunctionThe input() function is used to read user input from the keyboard. It's one of the first functions you'll use when making interactive Python programs.
input(prompt)
prompt (optional) – A string, shown to the user before input is read.string.name = input("What is your name? ")
print("Hello, " + name)
What is your name? Alice
Hello, Alice
age = input("Enter your age: ")
print("You are", age, "years old")
Enter your age: 25
You are 25 years old
Even if you type numbers, input() returns them as strings. You need to convert them manually using int() or float().
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum is:", num1 + num2)
Enter first number: 5
Enter second number: 10
Sum is: 15
int() or float()input()In Python coding interviews, input is often simulated using input(). Always remember that it returns strings and type conversion may be required.
input() pauses the program and waits for user inputstringint(), float(), or eval() to convert the input if neededWrite a program that asks the user for two numbers and prints the multiplication result.
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Product is:", a * b)
Connect what you noticed in the example with the idea below.
input() displays an optional prompt and returns the entered line as a string. The response is supplied as a fixture in this guided simulation.
input() returns an integer whenever the user types digits.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.