I am having an issue with my code. I need my code to restart and begin calculating from 0. However, since I declared my list as a global variable, It is not doing that. I do not know how to write my code so that it restarts properly.
By not doing so, I am getting incorrect results. As shown in the attached screenshot that i took.
Here is my code: salesList=[] def main(): print("Please enter the sales for each day of the week") while True: try: monday=float(input("Monday: $ ")) while monday < 0: print("[No Negative Numbers Allowed]") monday=float(input("Monday: $ ")) except ValueError: print("[Invalid Input]") continue else: break addToList(monday) while True: try: tuesday=float(input("Tuesday: $ ")) while tuesday < 0: print("[No Negative Numbers Allowed]") tuesday=float(input("Tuesday: $ ")) except ValueError: print("[Invalid Input]") continue else: break addToList(tuesday) while True: try: wednesday=float(input("Wednesday: $ ")) while wednesday < 0: print("[No Negative Numbers Allowed]") wednesday=float(input("Wednesday: $ ")) except ValueError: print("[Invalid Input]") continue else: break addToList(wednesday) while True: try: thursday=float(input("Thursday: $ ")) while thursday < 0: print("[No Negative Numbers Allowed]") thursday=float(input("Thursday: $ ")) except ValueError: print("[Invalid Input]") continue else: break addToList(thursday) while True: try: friday=float(input("Friday: $ ")) while friday < 0: print("[No Negative Numbers Allowed]") friday=float(input("Friday: $ ")) except ValueError: print("[Invalid Input]") continue else: break addToList(friday) while True: try: saturday=float(input("Saturday: $ ")) while saturday < 0: print("[No Negative Numbers Allowed]") saturday=float(input("Saturday: $ ")) except ValueError: print("[Invalid Input]") continue else: break addToList(saturday) while True: try: sunday=float(input("Sunday: $ ")) while sunday < 0: print("[No Negative Numbers Allowed]") sunday=float(input("Sunday: $ ")) except ValueError: print("[Invalid Input]") continue else: break addToList(sunday) total(salesList) cont() def addToList(sales): salesList.append(sales) return(sales) def total(salesList): total=0 for sales in (salesList): total += sales print("The Total Sales of the Week is ", total) def cont(): userInput=input("Do you want to add another week of sales and continue [Yes or No]? ") if (userInput=="Yes" or userInput=="yes" or userInput=="Y" or userInput=="y"): print("Program will now restart \n") main() else: print("Goodbye!") main()
@518nad Here bro, something for you to work on.
i dont know anything about python .. but if i was to use java, i would put all of ur condition in one big while loop.
You're just not including your initialization of total sales for each week.
Join our real-time social learning platform and learn together with your friends!