Book a free demo

๐Ÿ Python from scratch ยท Session 3

Talking to the user: input() and types: practice sheet

Name ____________________Date __________Score ____ / 24

โ† Back to the lesson

Part 1: Asking a question

Remember: input("question") shows the question, waits for an answer and gives it back. Store it in a variable.

  1. 1. Warm-up Ask for a name and print Welcome, <name>!

  2. 2. Core Ask for a colour, then an animal. Print: Your <colour> <animal> is ready.

  3. 3. Stretch ๐Ÿ”ฅ Ask for three words, then print them in reverse order on one line, separated by spaces.

Part 2: input() always gives text

Remember: input() gives back a string even if digits are typed. "11" + "5" is "115".

  1. 4. Warm-up Read two numbers and print their sum as the last line.

  2. 5. Core Read a whole number n. Print n joined with itself as text, then n doubled as a number, on the last two lines (for 5: 55 then 10).

  3. 6. Stretch ๐Ÿ”ฅ Read the side of a square. Print Area: โ€ฆ and Perimeter: โ€ฆ as the last two lines.

Part 3: Convert straight away

Remember: Convert as you read: age = int(input("Age? ")). Use int for whole numbers and float for anything with a decimal point.

  1. 7. Warm-up Read an age and print: Next year you will be <age + 1>.

  2. 8. Core Read a price (decimal) and a quantity (whole). Print Total: and the cost.

  3. 9. Stretch ๐Ÿ”ฅ Read a temperature in ยฐC and print it in ยฐF (F = C ร— 9 / 5 + 32) as the last line, just the number.

Part 4: Calculators that work for anyone

Remember: Put it together: read, convert, calculate, print. The program has no numbers of its own any more: it works for whatever is typed.

  1. 10. Warm-up Read two whole numbers and print their product as the last line.

  2. 11. Core Read a bill and the number of people. Print Each pays: and the share.

  3. 12. Stretch ๐Ÿ”ฅ Read a number of seconds and print it as minutes and seconds, like 2 min 10 sec, as the last line.

Activity 1: Age in the year 2040

  1. 13. Ask for a name and a birth year. The last two lines must be: Hi <name>! and In 2040 you will be <age>.

    1. Read the name.
    2. Read the birth year and convert it with int().
    3. Work out age = 2040 - year.
    4. Print the two lines with f-strings.
    name = input("Your name? ")
    year = input("Year you were born? ")
    
    # Step 2: convert year
    
    # Step 3: work out the age
    
    # Step 4: print two lines
    

Activity 2: Pizza party planner

  1. 14. Read the number of guests and how many slices each eats. A pizza has 8 slices. The last three lines must be: Slices: โ€ฆ, Pizzas: โ€ฆ (enough pizzas, rounded up) and Spare slices: โ€ฆ.

    1. Read guests and slices each, as whole numbers.
    2. slices = guests ร— slices each.
    3. pizzas = (slices + 7) // 8 rounds up to whole pizzas.
    4. spare = pizzas ร— 8 - slices.
    5. Print the three lines.
    guests = int(input("Guests? "))
    each = int(input("Slices each? "))
    
    # Step 2
    
    # Step 3
    
    # Step 4
    
    # Step 5
    

Activity 3: Story machine

  1. 15. Read a name, an animal, a place and a food. Print a story of at least three lines. The first story line must be exactly: Once upon a time, <name> the <animal> lived on <place>.

    1. Read the four words.
    2. Print the first line exactly as shown, with an f-string.
    3. Add at least two more lines that use the place and the food.
    4. Run it with silly words and read it aloud!
    name = input("A name? ")
    animal = input("An animal? ")
    place = input("A place? ")
    food = input("A food? ")
    
    # Step 2: the first line
    
    # Step 3: two more lines
    

Predict the output

Work it out on paper first, then type it into the lesson page to check.

  1. 16. What does this print?

    print("7" + "3")
  2. 17. What does this print?

    print(int("7") + 3)
  3. 18. What does this print?

    print(str(5) + "5")
  4. 19. What does this print?

    print(float("2.5") * 2)
  5. 20. What does this print?

    print(int(3.9))
  6. 21. What does this print?

    print("3" * 3)

Homework

  1. 22. Warm-up Read a number and print its square as the last line.

  2. 23. Core Read the length and width of a room (decimals allowed) and print the area as the last line.

  3. 24. Stretch ๐Ÿ”ฅ Read the marks of three tests. Print the total and then the average, as the last two lines.

Answer key

  1. name = input("Name? ")
    print(f"Welcome, {name}!")
  2. c = input("Colour? ")
    a = input("Animal? ")
    print(f"Your {c} {a} is ready.")
  3. a = input("1? ")
    b = input("2? ")
    c = input("3? ")
    print(c, b, a)
  4. a = int(input("a? "))
    b = int(input("b? "))
    print(a + b)
  5. n = input("n? ")
    print(n + n)
    print(int(n) * 2)
  6. s = int(input("Side? "))
    print(f"Area: {s * s}")
    print(f"Perimeter: {4 * s}")
  7. age = int(input("Age? "))
    print(f"Next year you will be {age + 1}.")
  8. p = float(input("Price? "))
    q = int(input("Qty? "))
    print(f"Total: {p * q}")
  9. c = float(input("C? "))
    print(c * 9 / 5 + 32)
  10. a = int(input())
    b = int(input())
    print(a * b)
  11. bill = float(input("Bill? "))
    n = int(input("People? "))
    print(f"Each pays: {bill / n}")
  12. s = int(input("Seconds? "))
    print(f"{s // 60} min {s % 60} sec")
  13. name = input("Your name? ")
    year = int(input("Year you were born? "))
    age = 2040 - year
    print(f"Hi {name}!")
    print(f"In 2040 you will be {age}.")
  14. guests = int(input("Guests? "))
    each = int(input("Slices each? "))
    slices = guests * each
    pizzas = (slices + 7) // 8
    spare = pizzas * 8 - slices
    print(f"Slices: {slices}")
    print(f"Pizzas: {pizzas}")
    print(f"Spare slices: {spare}")
  15. name = input("A name? ")
    animal = input("An animal? ")
    place = input("A place? ")
    food = input("A food? ")
    print(f"Once upon a time, {name} the {animal} lived on {place}.")
    print(f"Every day {name} ate {food} for breakfast.")
    print(f"One day the {food} ran out on {place}...")
  16. 73
  17. 10
  18. 55
  19. 5.0
  20. 3
  21. 333
  22. n = int(input("Number? "))
    print(n * n)
  23. l = float(input("Length? "))
    w = float(input("Width? "))
    print(l * w)
  24. a = int(input())
    b = int(input())
    c = int(input())
    print(a + b + c)
    print((a + b + c) / 3)
Chat on WhatsApp
Esc

Try