[go: up one dir, main page]

0% found this document useful (0 votes)
10 views3 pages

Worksheet 1

The document is a worksheet focused on Python programming, specifically on sub-programs (functions). It includes tasks for predicting outcomes based on user inputs, identifying functions, and modifying code to handle different cases. Additionally, it challenges users to enhance a math program to perform multiple operations and validate user input.

Uploaded by

phuong031979
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Worksheet 1

The document is a worksheet focused on Python programming, specifically on sub-programs (functions). It includes tasks for predicting outcomes based on user inputs, identifying functions, and modifying code to handle different cases. Additionally, it challenges users to enhance a math program to perform multiple operations and validate user input.

Uploaded by

phuong031979
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Worksheet 1

Full name:........................................ Class:.................

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?

Test number Data entered Expected outcome


1 morning
2 night
3 Morning
4 hello

2. How many sub-programs (functions) does this program have? What are
their names?

What user input will make each sub-program run?

How can we modify the program to accept both “Morning” and


“morning”?

SILVER: The program below uses two sub-programs.

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

print("Welcome to the math program!")


print("I can add or divide two numbers.")
choice = int(input("Press 1 to add or 2 to divide: "))

if choice == 1:
result = add()
elif choice == 2:
result = divide()

print("The result is", _______________)

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.

You might also like