Eman
Eman

Reputation: 25

I can't seem to get out of the loop on this program on python

I am about 5 weeks in my very first programming class, and this is still a bit difficult for me. I was wondering if anyone could help me out.

I guess I can't seem to figure out what I am Doing wrong, after finishing the input commands for the 7 days, it just goes back to the first day. This is my first time posting here so I apologize if I put in almost all of the code, I am just doing it for reference to see if maybe its something above of below the while loops that is causing my program to repeat itself. Thanks for any help in advance!

keepgoing = "y"

while keepgoing == "y":


    while True:
        try:
            sundaySales = int(input("Enter Sunday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:

            break

    while True:
        try:
            mondaySales = int(input("Enter Monday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:

            break

    while True:
        try:
            tuesdaySales = int(input("Enter Tuesday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:

            break

    while True:
        try:
            wednesdaySales = int(input("Enter Wednesday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:

            break

    while True:
        try:
            thursdaySales = int(input("Enter Thursday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:

            break

    while True:
        try:
            fridaySales = int(input("Enter Friday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:

            break

    while True:
        try:
            saturdaySales = int(input("Enter Saturday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:
        return True
Size=7

Sales=[sundaySales, mondaySales, tuesdaySales, wednesdaySales, thursdaySales, fridaySales, saturdaySales]                  

totalWeeklySales = sundaySales+mondaySales+tuesdaySales+wednesdaySales+thursdaySales+fridaySales+saturdaySales
sentence = "This week's total sales are ${} ". format(totalWeeklySales)
print (sentence)
import totalOutcome
totalOutcome.totalOutcome(totalWeeklySales)

keepGoing = input("Do you want to run this again? (Enter y)= ")

if keepGoing != "y":
    print ("Great job this week!")

Upvotes: 2

Views: 208

Answers (2)

YKL
YKL

Reputation: 542

I just simply modified your code and re-formatted a little bit:

keepgoing = "y"

while keepgoing == "y":

    while True:
        try:
            sundaySales = int(input("Enter Sunday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:
            break

    while True:
        try:
            mondaySales = int(input("Enter Monday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:
            break

    while True:
        try:
            tuesdaySales = int(input("Enter Tuesday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:
            break

    while True:
        try:
            wednesdaySales = int(input("Enter Wednesday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:
            break

    while True:
        try:
            thursdaySales = int(input("Enter Thursday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:
            break

    while True:
        try:
            fridaySales = int(input("Enter Friday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:
            break

    while True:
        try:
            saturdaySales = int(input("Enter Saturday's total sales: $"))
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
        else:
            break
    Size = 7

    Sales = [sundaySales, mondaySales, tuesdaySales,
             wednesdaySales, thursdaySales, fridaySales, saturdaySales]

    totalWeeklySales = sundaySales + mondaySales + tuesdaySales + \
        wednesdaySales + thursdaySales + fridaySales + saturdaySales
    sentence = "This week's total sales are ${} ". format(totalWeeklySales)
    print (sentence)
     import totalOutcome
     totalOutcome.totalOutcome(totalWeeklySales)

    keepgoing = input("Do you want to run this again? (Enter 'y')= ")

    if keepgoing != "y":
        print ("Great job this week!")

Please be aware that:

  • The naming of variables is not good, it's better to use sunday_sales than sundaySales, sales_list than Sales, etc. Naming Conventions
  • There's a return True in you raw code at the end of calculating Saturday sales, in fact it should be break. (BTW, while True is really a terrible practice, please avoid using it if possible) Why while(true) is bad practice?
  • Code indent is not correct
  • Variable is not consistent: keepgoing vs keepGoing
  • When you input for Do you want to run this again? (Enter 'y')=, please make sure the string you inputted with quote "abc" or 'abc', not abc, otherwise there would be error raised since Python treats input as raw_input
  • As mentioned by @Lord of dark, import in loop is bad, please import at the beginning of the file or function definition(if there's cyclic imports) Python Importing and Circular (or cyclic) imports in Python

Upvotes: 2

Fabich
Fabich

Reputation: 3069

Here are a few pieces of advice to improve your code :

  • Do not use return True outside a function. You can only use return to leave a function. To leave a while loop use break

  • Import package at the beginning of your program (move import totalOutcome to the top) so it is not imported at every loop.

  • You should put your end code inside the while loop. Right now you never change the the value keepgoing so the loop will loop forever :

  • You should not manually write code for every day, you should iterate over a list of the days and store every result in a list.

Here is a more compact version of this code :

days=['Sunday','Monday','Tuesday','Wednesday','Thurday','Friday','Saturday']

while True:
  Sales = []
  for day in days:
    while True:
      try:
        daylySales = int(input("Enter "+day+"'s total sales: $"))
        Sales.append(daylySales)
      except ValueError:
        print("Sorry, I didn't understand that.")
        continue
      else:
        break

  totalWeeklySales = sum(Sales)
  sentence = "This week's total sales are ${} ". format(totalWeeklySales)
  print (sentence)

  keepGoing = input("Do you want to run this again? (Enter y)= ")

  if keepGoing != "y":
      print ("Great job this week!")
      break

I tried do keep you variables name as much as possible.

Upvotes: 2

Related Questions