2

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!")
Eman
  • 25
  • 4
  • 1
    There is no way that the above code runs for you without errors. Please make sure that what your asking corresponds to your actual problem. And after you do that: you'll need to break out of the enclosing while loop, which you never seem to be doing. – Andras Deak -- Слава Україні Feb 08 '17 at 01:14
  • Change your first `While` to `while`. Can you please indent correctly your program ? – Fabich Feb 08 '17 at 01:21
  • `While keepgoing == "y":` isn't valid Python (note the capital letter), and none of the body of the loop is indented. – John Coleman Feb 08 '17 at 01:21
  • Are you trying to get infinite loops? because this is how you get infinite loops – Justin M. Ucar Feb 08 '17 at 01:31
  • Sorry I honestly, did not realize any of the things mentioned above. It's an online class and we kinda just get thrown into making a python program without learning it first. – Eman Feb 08 '17 at 01:32
  • Also, the code IS indented on my computer, I just copied it wrong on the site. Sorry again! – Eman Feb 08 '17 at 01:36
  • 2
    The problem is that the code that you actually posted won't run at all, but you seem to be asking about code which runs, but not as you expect. Thus -- the code that you posted isn't the code that you are running. Please edit your question so that it shows your actual code. Since the problem is almost certainly one of indentation, the fact that your way of copying the code to Stack Overflow has destroyed the indentation makes your question impossible to answer. Delete this unsuccessful copy, paste your code again, select it, then hit the `{}` icon. – John Coleman Feb 08 '17 at 01:37
  • @JohnColeman thank you for that! I believe I have correctly copied it to Stack Overflow – Eman Feb 08 '17 at 01:46
  • @EmmanuelR Yes, you did. This isn't a bad first question for Stack Overflow. Good luck with learning Python. – John Coleman Feb 08 '17 at 01:49
  • @JohnColeman thank you so much! – Eman Feb 08 '17 at 01:57

2 Answers2

2

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
Community
  • 1
  • 1
YKL
  • 542
  • 2
  • 9
2

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.

Fabich
  • 2,768
  • 3
  • 30
  • 44