CodeBlanks Logo Icon CodeBlanks

Summary - Input and output

Combine input, conversion and formatting to write an interactive menu.

Summary - Input and output

In this group you learned four core tools:

  • input() — reads a string from the user
  • int() / float() — convert a string to a number
  • print() sep/end — format the output
  • f-string — embeds an expression in a string
  • sys.argv — gives command-line arguments

Now you will do two short tasks that combine them: one with interactive input, one with a command-line argument.

Practice Interactive age calculator

Assign input("Ad: ") to name (user types Ayşe). Assign int(input("Yaş: ")) to age (user types 30). Then write print(f"{name} gelecek yıl {age + 1} yaşında").

Starter code

main.py
# read name and age
# print next year's age with f-string

Sample solution

main.py
name = input("Ad: ")
age = int(input("Yaş: "))
print(f"{name} gelecek yıl {age + 1} yaşında")

Expected output

Ad: Yaş: Ayşe gelecek yıl 31 yaşında

Practice Print double with sys.argv

Write import sys. Assign sys.argv[1] if len(sys.argv) > 1 else "5" to val. Assign int(val) to num. Then write print(num * 2). The script runs with no arguments.

Starter code

main.py
# bring in the sys module
# use "5" if no argument, convert to int
# double and print

Sample solution

main.py
import sys
val = sys.argv[1] if len(sys.argv) > 1 else "5"
num = int(val)
print(num * 2)

Expected output

10

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 How are input() and int() used together?

  • int(input(...)) Correct answer
  • int.input(...)
  • input(int(...))
  • input->int(...)

Explanation: int(input(...)) works outside-in: first input is read, then converted.

2 Complete the print sep

Question
print("a", "b", "c", sep=____)
Answer
print("a", "b", "c", sep=",")

Explanation: sep="," joins values with commas.

All lessons in Beginner