In Python 3, the raw_input()
function has been renamed to input()
. To use input()
in Python 3, you can simply use the following code:
name = input("What is your name? ")
This code will prompt the user with the string “What is your name?” and then wait for the user to enter a response. The response will be stored in the name
variable.
Here is an example of how you could use this code in a complete program:
# Ask the user for their name
name = input("What is your name? ")
# Print a greeting to the user
print("Hello, " + name + "!")
When you run this program, it will ask the user for their name, and then print a greeting using their name.
Note that in Python 3, input()
always returns a string, even if the user enters a number. If you want to use the value entered by the user as a number (integer or floating-point), you will need to convert it to a number using the int()
or float()
function. For example:
# Ask the user for their age
age = input("How old are you? ")
# Convert the age to an integer
age = int(age)
# Print the age in one year
print("In one year, you will be " + str(age + 1) + " years old.")
In this example, the age
variable will be set to the string entered by the user. The int()
function is then used to convert the string to an integer, and the resulting integer is used in a calculation to print the user’s age in one year.