Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is it possible to get an input onto a variable while having it in a loop?

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!")

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}.")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading