Worksheet 1
Worksheet 1
BRONZE: Look at the following Python program that uses sub-programs. Answer
the questions below:
1. Follow the program to predict the expected outcome when the program is
run using each of the following inputs?
2. How many sub-programs (functions) does this program have? What are
their names?
The program starts by welcoming the user and asking them to choose a calculation.
One sub-program performs addition, while the other performs division. The chosen
sub-program runs, returns the result, and the program prints the final answer.The
program starts by welcoming the user and asking them to choose a calculation. One
sub-program performs addition, while the other performs division. The chosen sub-
program runs, returns the result, and the program prints the final answer.
Fill in the gaps to complete the program code so that it will run correctly (use
different font color).
def add():
num1 = _______________(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
answer = num1 _______________ num2
return _______________
def divide():
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if num2 == 0:
return "Cannot divide by 0!"
return num1 / num2
if choice == 1:
result = add()
elif choice == 2:
result = divide()
GOLD:
- Run the program using the TutorialsPoint.com online python compiler.
- Add code so the program can add, subtract, multiply and divide.
- Check that your program works as intended, and paste your code in the box
below.
- Challenge: Edit the code to check if the input is an integer. If the user enters an
invalid character (e.g. a letter), ask for re-entry.