Python Quest

Track 1 · Lesson 7 of 11

Asking for input

Make programs interactive with input(), and convert the text you receive into numbers with int() and float().

About 30 minBeginner4 exercisesFree

You will be able to

  • Ask the user a question with input() and store the answer in a variable
  • Explain why input() always gives you text, even when the user types a number
  • Convert input to whole numbers with int() and to decimals with float()
  • Write clear prompts and combine input with calculations

Every program so far has printed the same thing every time it ran. Real programs respond to the person using them: a parking machine asks how many hours, a shop till asks for quantities, a form asks for your name. In this lesson your programs start asking questions.

input()

input() is a function that pauses the program, waits for the user to type something and press Enter, and then gives back what they typed. You store the answer in a variable so you can use it.

The text inside the parentheses is the prompt: the question shown to the user. End it with a space (or a colon and a space) so the answer doesn't touch the question.

Asking for a name
name = input("What is your name? ")
print("Welcome to Cedar Market,", name)
Input given to the program: "Huda"

Try it: Run it again and type a different name when the program asks.

When the program reaches input(), it stops and waits. Whatever the user types (here, Huda) becomes the value of name, and the program continues with the next line.

input() always gives you text

This is the most important fact in the lesson: input() always returns a string, even if the user types digits. Typing 5 gives you the text "5", not the number 5. Watch what happens if you forget:

Two inputs joined, not added
first = input("First number: ")
second = input("Second number: ")
print("Sum:", first + second)
Input given to the program: "5", "3"

Try it: Predict the result before you run it. Then try entering 10 and 20.

The program printed 53. Both values are strings, and + joins strings, exactly as you saw in Text and strings. To do arithmetic, you need to convert the text to a number first.

Converting text to numbers

  • int(text) converts text such as "42" to the whole number 42.
  • float(text) converts text such as "8.5" (or "8") to the decimal number 8.5 (or 8.0).

You can convert as soon as you read the value by putting input() inside int() or float(). Python works from the inside out: first input() gets the text, then int() converts it.

Converting immediately
quantity = int(input("How many tickets? "))
price = float(input("Price per ticket: "))
total = quantity * price
print(f"Total: {total:.2f} SAR")
Input given to the program: "3", "42.5"

Try it: Run it and enter 2 and 19.99. Then try entering 2.5 tickets and read the error.

Use int() for things you count in whole units (tickets, people, items) and float() for measurements and prices that can have decimals.

If the text isn't a valid number, the conversion fails with a ValueError. int("2.5") fails because 2.5 isn't a whole number, and int("five") fails because "five" isn't digits. Spaces around the digits are fine: int(" 42 ") is 42. You'll learn to handle invalid input gracefully in later tracks.

A decimal can't become a whole number with int()
people = int(input("Number of people: "))
print(people)
Input given to the program: "2.5"

Try it: Change int to float and run it again with the same input.

Predict the output

a = input("a: ")
b = int(input("b: "))
print(a * 3)
print(b * 3)

Guided practice · ●difficulty 1 of 5 · about 3 min

Greet a customer by name

Ask for the customer's name (for example with the prompt Your name: ), then greet them:

Welcome to Cedar Market, Lina!

where Lina is whatever the customer typed. There is no space before the !.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Challenge · ●●difficulty 2 of 5 · about 4 min

Work out a parking fee

A car park charges 5 SAR per hour. Ask how many hours the car was parked, then print the fee. For an input of 4:

Parking fee: 20 SAR

Hours are always whole numbers.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Challenge · ●●difficulty 2 of 5 · about 5 min

Price fruit by weight

At a market stall, fruit is priced per kilogram. Ask for two values, in this order:

  1. the price per kilogram,
  2. the weight in kilograms.

Both can have decimals. Print the cost with two decimal places. For inputs 8.5 and 1.5:

Cost: 12.75 SAR

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Debugging · ●●difficulty 2 of 5 · about 5 min

Fix the seat counter

A training centre uses this program to see how many seats are left in a room. It asks for the room capacity and the number of seats booked, but it stops with a TypeError. For inputs 40 and 25 it should print:

Seats left: 15

Fix it, keeping the two questions in the same order.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Summary

  • input(prompt) shows the prompt, waits for the user, and returns what they typed.
  • input() always returns a string, even when the user types digits.
  • int() converts text to a whole number and float() to a decimal; invalid text raises a ValueError.
  • Convert as you read: quantity = int(input("Quantity: ")).