109 lines
3.3 KiB
Python
109 lines
3.3 KiB
Python
def get_name():
|
|
collect = True
|
|
while collect:
|
|
name = input("Enter the activity: ")
|
|
if len(name) > 10:
|
|
print("Name too long. Please try again.")
|
|
continue
|
|
else:
|
|
collect = False
|
|
return name
|
|
|
|
def get_amount():
|
|
collect = True
|
|
while collect:
|
|
amount = float(input("Enter activity hourly rate in pence: "))
|
|
# if less than 99 or greater than 599
|
|
if amount < 99 or amount > 599:
|
|
print("Amount out of range. Please try again.")
|
|
continue
|
|
else:
|
|
collect = False
|
|
return amount
|
|
|
|
def print_activities(data):
|
|
print("\nActivity - Amount")
|
|
print("-" * 15)
|
|
for activity in data:
|
|
print(f"{activity[0]} - £{activity[1]/100}")
|
|
|
|
def get_student_name():
|
|
"""Ask for and return a student's name."""
|
|
return input("Enter student name: ")
|
|
|
|
def get_activity_choice(activities):
|
|
"""
|
|
Show the available activities and ask the user to choose one.
|
|
Retries until a valid activity name (from the ex20 list) is entered.
|
|
Returns the chosen activity name as a string.
|
|
"""
|
|
activity_names = [a[0] for a in activities]
|
|
collect = True
|
|
while collect:
|
|
print("Available activities:", ", ".join(activity_names))
|
|
choice = input("Enter the activity this student did: ")
|
|
if choice not in activity_names:
|
|
print("Not a valid activity. Please try again.")
|
|
continue
|
|
else:
|
|
collect = False
|
|
return choice
|
|
|
|
def get_hours():
|
|
"""Ask for the whole number of hours spent on the activity. Retries until valid."""
|
|
collect = True
|
|
while collect:
|
|
hours_input = input("Enter number of hours spent (whole number): ")
|
|
if not hours_input.isdigit():
|
|
print("Please enter a whole number.")
|
|
continue
|
|
else:
|
|
collect = False
|
|
return int(hours_input)
|
|
|
|
def get_rate_for_activity(activities, activity_name):
|
|
"""Look up the hourly rate (in pence) for a given activity name."""
|
|
for activity in activities:
|
|
if activity[0] == activity_name:
|
|
return activity[1]
|
|
|
|
def print_students(students):
|
|
"""Print student data in tabular format with column headings."""
|
|
print("\nStudent Name - Activity - Hours - Total Raised")
|
|
print("-" * 45)
|
|
for student in students:
|
|
name, activity, hours, total = student
|
|
print(f"{name} - {activity} - {hours} - £{total/100}")
|
|
|
|
|
|
def main():
|
|
# build activities table
|
|
activities = []
|
|
run = True
|
|
while run:
|
|
activity = get_name()
|
|
amount = get_amount()
|
|
activities.append([activity, amount])
|
|
user_choice = input("Would you like to add another activity? (y/n): ")
|
|
if user_choice.strip().lower() == "n":
|
|
run = False
|
|
|
|
print_activities(activities)
|
|
|
|
# collect student data
|
|
num_students = int(input("\nHow many students do you want to enter? "))
|
|
|
|
students = []
|
|
for _ in range(num_students):
|
|
student_name = get_student_name()
|
|
activity_choice = get_activity_choice(activities)
|
|
hours = get_hours()
|
|
rate = get_rate_for_activity(activities, activity_choice)
|
|
total = hours * rate
|
|
students.append([student_name, activity_choice, hours, total])
|
|
|
|
print_students(students)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |