input() shows a question, waits for an answer and hands it back as text, even when the person types digits. So typing 3 into count * 2 gives 33: a string times 2 is the text twice. To do arithmetic with the answer, turn it into a number first with int() or float().
Try it
main.py
When it asks, try typing "3".
Output
Press Run and what your program prints shows up here.
Easy to get wrong
Thinks input() gives back a number when digits are typed.
Thinks multiplying text by a whole number is an error.
Thinks repeating text with * puts a space between the copies.
Python's input() function shows a prompt, waits for the user to type something and press Enter, and gives back what they typed. It always gives it back as a string (str), even when the user types a number. So if you want to do arithmetic with the answer, you have to convert it first with int() for whole numbers or float() for decimals. Forgetting this step is one of the most common causes of wrong answers and crashes in a beginner's first programs.
How it works
Here the user types 4. What comes back is the text "4", one character long, and the arithmetic operators treat it as text:
count + count joins two strings, giving 44. count * 3 repeats the string three times, giving 444. Neither line crashes, which is what makes this mistake slippery: the program runs and prints a confident wrong answer. Only after int(count) is there a number, and crates * 3 gives 12.
The usual pattern converts the answer on the same line it is read, so the variable never holds the text at all:
Python works from the inside out: input() runs first and hands its text to float() or int(). Note the space at the end of each prompt. input() prints the prompt exactly as written, so without that space the answer would be typed hard up against the question mark.
If the user types something the conversion can't read, such as 2.5 into int(), or presses Enter without typing anything (which gives the empty string ""), the program stops with a ValueError.
Common mistakes
Thinking input() gives back a number when digits are typed. It never does. input("Age? ") + 1 is a TypeError, because it tries to add a number to text. Wrap the call: int(input("Age? ")).
Expecting text times a number to be an error. It isn't: a string times a whole number repeats the string. So forgetting to convert an answer before multiplying gives a wrong result with no error to warn you.
Expecting repeated text to have spaces between the copies."3" * 2 is "33", not "3 3". Repetition puts the copies side by side with nothing between them. If you want a gap, it has to be part of the string being repeated.
Related
The two conversions have their own pages: int() and float(). When you are not sure what a variable holds, type() will tell you. To keep asking until the user types something valid, see defensive input.