completed exercise 20-22
This commit is contained in:
parent
1b43e8deb6
commit
51e91db895
109
21 Fundraiser part 2/exercise-01.py
Normal file
109
21 Fundraiser part 2/exercise-01.py
Normal file
@ -0,0 +1,109 @@
|
||||
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()
|
||||
BIN
21 Fundraiser part 2/task.png
Normal file
BIN
21 Fundraiser part 2/task.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 87 KiB |
1
22 Develop the simple game board/exercise-01.py
Normal file
1
22 Develop the simple game board/exercise-01.py
Normal file
@ -0,0 +1 @@
|
||||
create_grid, print_grid, main = lambda : " " + " ".join(str(i) for i in range(1, 9)) + "\n" + "\n".join(f"{r+1} " + " ".join("*" if (r, c) == (7, 0) else ("X" if (r+c) % 2 == 0 else "O") for c in range(8)) for r in range(8)), lambda grid : print(grid), lambda: print_grid(create_grid()) ; main()
|
||||
BIN
22 Develop the simple game board/task.png
Normal file
BIN
22 Develop the simple game board/task.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 48 KiB |
Loading…
Reference in New Issue
Block a user