A function, activitySuggestions, takes a String as a parameter. The String represents a day. It displays a suggested activity for the day:
Here is the program, written in PYTHON:
#function definition
def activitySuggestions(day):
if day == "Saturday" or day == "Sunday":
print(f"It's {day} so take your family to a restaurant!")
elif day == "Monday" or day == "Wednesday":
print(f"It's {day} so get some exercise after work!")
elif day=="Tuesday" or day=="Thursday":
print(f"It's {day} so stay home and cook dinner and watch tv")
elif day == "Friday":
print(f"It's {day}! Take your loved ones to the cinema!")
else:
print(f"{day} is not a valid day! Try again.")
Copy this code into a Python file. Test it by calling it. When you call it, remember to give it some data.
You could write a program that asks the user to enter a day and it sends the user's data to the function when called.
The following program is entended to simulate a dice game:
#import the random library file
import random
#generate a random number and assign to a variable
compNum = random.randint(1,6)
#get the user's number
playerNum = int(input("Enter a number from 0 to 6: "))
#call a function to display the winner
winner(compNum, playerNum)
print("End of program")
Design the winner function.
Its signature is:
def winner(c,p):
If c is greater that p, Computer wins!! is displayed along with both numbers.
elif p is greater than c, Player wins!! is displayed along with both numbers.
else Draw!! is displayed along with both numbers.
A computer program uses both global variables and function calls with parameters.
A teacher uses a computer program to determine grades.
For each of her 10 students, she enters a score out of 100.
Then, the grade() function is called to display the grade (see table below).
Then a function is called
#global variable
grade = ''
for i in range(10):
score = int(input("Enter the score: "))
grade(score) #updates the global variable using score
advice() #needs access to the current grade ie the global variable
print("End of program")
Grade Table
| Score | Grade |
|---|---|
| >80 | A |
| >70 | B |
| >60 | C |
| <=60 | D |
The grade() function takes a number as a parameter. It updates the global variable (see table) and displays the grade. Remember to use the global keyword in the function design.
The advice() function prints some advice depending on the grade eg Study harder! (use your own ideas)
Design the two functions and test your program to see if it works as intended!