I am trying to get this function into a while loop but am having trouble with it. Any advice would be appreciated!
def get_totalspent():
print("Please enter the amount you spent on coffee for each day of the week\n")
weekdays = ["sunday","monday","Tuesday","wednesday","thursday","friday","saturday"]
sunday=eval(input("Enter amount spent for sunday:"))
monday=eval(input("Enter amount spent for monday:"))
tuesday=eval(input("Enter amount spent for tuesday:"))
wednesday=eval(input("Enter amount spent for wednesday:"))
thursday=eval(input("Enter amount spent for thursday:"))
friday=eval(input("Enter amount spent for friday:"))
saturday=eval(input("Enter amount spent for saturday:"))
#accumulate total
totalSpent = sunday monday tuesday wednesday thursday friday saturday
print("You spent:", totalSpent, "dollars on coffee this week. You may want to cut back on the caffine!")
CodePudding user response:
You don't need to define many variables. Instead, you can use a list:
weekdays = ["sunday","monday","Tuesday","wednesday","thursday","friday","saturday"]
amounts = []
for day in weekdays:
amounts.append(float(input(f"Enter amount spent for {day}: ")))
print(f"You spent ${sum(amounts):.2f}.")