Practical Challenges 7

<< Previous: FunctionsNext: Text files >>

About the challenges

The challenges below allow you to apply your knowledge of the topics up to this point.

Challenge 1: Shopping list

Write a program that has a shopping list. The program should have one subroutine that lets the user add an item to the list, another subroutine that displays all items currently on the list, another that lets the user remove something from the list and a final subroutine that clears all items out of the list. The program should operate from a main menu that calls each of the subroutines. This menu should also have an option to stop the program.





def add_item(shop_list):
    item = input("Enter an item to add to the shopping list: ").title()
    shop_list.append(item)
    return shop_list

def view_list(shop_list):
    print("Shopping list")
    for item in list:
        print(item)

def remove_item(shop_list):
    view_list(shop_list)
     choice = input("Which item do you want to remove")
    shop_list.remove(choice)
    return shop_list

def empty_list(shop_list):
    shop_list.clear()
    return shop_list

shopping_list = []
choice = 0
while choice != 5:
     choice = int(input("Choose 1. Add item 2. View items 3. Remove item 4. Clear list or 5. End program"))
    if choice == 1:
        shopping_list = add_item(shopping_list)
    elif choice == 2:
        view_list(shopping_list)
    elif choice == 3:
        shopping_list = remove_item(shopping_list)
    elif choice == 4:
        shopping_list = empty_list(shopping_list)
    elif choice == 5:
        print("Thanks for using the shopping list program")

Challenge 2: Circumference or area

Write a program with two subroutines, one that calculates the area of a circle and one that calculates the circumference of the circle. The main program should ask the user if they want to know the circumference or area of a circle, or both and then call the appropriate subroutine or subroutines.





def calc_circ(num):
    circ = 2 * 3.14 * num
    print(f"The circumference is {circ}")

def calc_area:
    area = 3.14 * radius * radius
    print(f"The area is {area}")

radius = int(input("Enter the radius of the circle: "))
choice = input("Choose 1. Calculate the circumference 2. Calculate the area 3. Calculate area and circumference")
if choice == "1":
    calc_circ(radius)
elif choice == "2":
    calc_area(radius)
elif choice == "3":
    calc_circ(radius)
    calc_area(radius)

Challenge 3: Savings monitor

Write a program with subroutines for add savings, withdraw savings and view savings total. The savings monitor should let the user add more savings, withdraw savings, view the balance of their savings or quit the program from a menu system that calls the subroutines.





def deposit(savings):
    amount = float(input("Enter amount to be saved: "))
    savings = savings + amount
return savings

def withdraw(savings):
    amount = float(input("Enter amount to be withdrawn: "))
    savings = savings - amount
return savings

def view(savings):
    print(f"Current savings:{savings}")

savings = 0
choice = 0
while choice != 4:
choice = int(input("Choose 1. Add savings 2. Withdraw savings 3. View savings 4. Close program"))
if choice == 1:
    savings = deposit(savings)
elif choice == 2:
    savings = withdraw(savings)
elif choice == 3:
    view(savings)
elif choice == 4:
    print("Thanks for using the savings monitor")
<< Previous: FunctionsNext: Text files >>

© All materials created by and copyright S.Goff