Python Programming · Middle Stage (6–8) · 2 courses, 21 worked problems, 36 practice questions
A program needs a way to talk to the person using it. In Python, print() shows text and numbers on the screen, and input() reads what the user types. The value from input() is always a string, so you change it to a number with int() or float() before doing arithmetic. An f-string such as f"Speed: {v} m/s" puts values into a sentence, and {v:.2f} shows a value to two decimal places. With these two functions, a script becomes a small interactive tool, such as a unit converter or a quiz.
input() and float()Choose an answer, then check it. Each one shows a full explanation.
A greeting program asks for a name and then for a number. The user types Ann, presses Enter, types 3 and presses Enter again.
name = input()
n = int(input())
print(name[0] * n, name + str(n), sep=":")
What is shown on the screen?
The user types three numbers on one line, with a space between them: 4 2 9. The program cuts the line into pieces.
nums = input().split()
print(nums[0] + nums[-1], int(nums[0]) + int(nums[-1]), len(nums))
What does the program print?
A function asks a question with input() and a prompt. The user types 4 and presses Enter.
def ask(prompt):
reply = input(prompt)
return reply * 2
print(ask("n? "), "end")
Do not count the characters the user types. What text does the program itself write on the screen?
Each one has a hint, the key idea and a full worked solution.
A function prints a value with its own end, and returns another value. Two calls are the arguments of one print() with sep.
def tag(x):
print(x, end="-")
return x + 5
print(tag(1), tag(2), sep="|")
What does the program print?
A program calls print() three times with keyword arguments, and then once with nothing.
print("A" + "B", "C" * 2, sep=",", end="/")
print(3, 4, sep="" + "-", end="")
print("!", 5 + 5, end="")
print()
What does the program print?
A program reads two lines from the keyboard. The first line entered is 10 and the second line is 9.
a = input()
b = input()
print(a > b, int(a) > int(b), max(a, b), a + b == "109")
What does the program print?
A program has three input() lines, but the Input box has only two lines. What happens?
The user types 2.5. What does print(float(input()) * 2) show?
int() turns text into a number. What does print(int("7") * 3) show?
A program has age = int(input()), and the user types 12.5. What happens?