If you’ve been following my tutorials, you may be thinking: “Luke, this is all interesting, but what is the use of all of this if our code is not interactive?” After all, a movie is interesting but a video game is usually more engaging.
I’m going to get straight to the point this time. The code to take a user’s input is:
Where keyword is usually the question you want to ask. For example, if we wanted to ask a user their name, we could type:
print(input(“what is your name? >>>”))
Try this yourself in python or here. If you entered your name, this should have printed it on the next line. But what if we want to store the data for later? We can store the data in a variable! We can use:
name = input(“What is your name? >>>”)
Than if we want to greet the user we can type:
print(“Welcome ” + name)
Try it yourself
Try running the code below. Did it work? Try changing the code to ask for the first name and last name separately. Than print “Welcome” followed by their full name.
l_name =input(“What is your last name? >>>”)
print(“Welcome ” + f_name + ” ” + l_name)
Now you know how to take input in python and save it for later. In the next lesson we will be making our first game.