In the previous sections of this topic, we discussed user input, but did not really explain how user input is obtained. In Python, the user types one line of input at a time. You should use the
function to actually obtain the next line of input from the user. The input()
input()
always gives back a String.
The next example demonstrates:
input()
multiple times, you can access multiple lines of input. The first call to input()
gets the first line, the second gets the second line, et cetera.input()
can be converted to an int
or a float
For the next exercise, you are asked to debug a program that is not working, and make it work. Note that the bug is not a typo, but rather a logical bug: the program was not correctly designed to do its job, so you must redesign it a bit.
As you have seen so far, when we want to see a result appear on the screen, we use print(). This is a function that most of your programs will have. Here are a few examples of how you can print:
print("I","love","turtles")
You can print multiple things each one separated by a comma. On the screen, each of these words will be printed one after another on the same line, all separated by a space.
>> I love turtles
You can also print numbers and variables.
shoe_size = 17
shoes_bought = 19
greeting = "hello"
print(greeting, "you bought", shoes_bought, "shoes of size", shoe_size)
This will join these strings, numbers and variables into one message along one line, all separated by spaces.
>> hello you bought 19 shoes of size 17
You can even print the result of calculations.
shoe_size = 7
print("Your shoe size is:", shoe_size + 10)
The two numbers will be added and the result will be printed:
>> Your shoe size is: 17
Lastly, you can certainly just print one thing at a time if you’d like each print statement will appear in a new line.
meaning_of_life = 42
print("hello")
print(17)
print("the meaning of life is:")
print(meaning_of_life)
and the resulting printed statements will be:
hello
17
the meaning of life is:
42