简体   繁体   English

我似乎无法摆脱这个程序在 python 上的循环

[英]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.我的第一堂编程课大约有 5 周时间,这对我来说仍然有点困难。 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.我想我似乎无法弄清楚我做错了什么,在完成了 7 天的输入命令后,它又回到了第一天。 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.这是我第一次在这里发帖,所以如果我输入了几乎所有的代码,我深表歉意,我只是为了参考而做它,看看它是否可能在导致我的程序重复的 while 循环下方。 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!")

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变量的命名不好,用sunday_salessundaySalessales_listSales ,等等。 命名约定
  • There's a return True in you raw code at the end of calculating Saturday sales , in fact it should be break .在计算Saturday sales的最后,您的原始代码中有一个return True ,实际上它应该是break (BTW, while True is really a terrible practice, please avoid using it if possible) Why while(true) is bad practice? (顺便说一句, while True是一种糟糕的做法,请尽可能避免使用它) 为什么 while(true) 是不好的做法?
  • Code indent is not correct代码缩进不正确
  • Variable is not consistent: keepgoing vs keepGoing变量不一致: keepgoing vs keepGoing
  • When you input for Do you want to run this again? (Enter 'y')=当你输入Do you want to run this again? (Enter 'y')= 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 Do you want to run this again? (Enter 'y')= ,请确保您输入的字符串带有引号"abc"'abc' ,而不是abc ,否则会出现错误,因为 Python 将输入视为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正如@Lord of dark 所提到的,循环导入不好,请在文件或函数定义的开头导入(如果有循环导入) Python 导入循环(或循环)导入 Python

Here are a few pieces of advice to improve your code :以下是一些改进代码的建议:

  • Do not use return True outside a function.不要在函数外使用return True You can only use return to leave a function.您只能使用return离开函数。 To leave a while loop use break要留下一个 while 循环,请使用break

  • Import package at the beginning of your program (move import totalOutcome to the top) so it is not imported at every loop.在程序开始时导入包(将import totalOutcome移到顶部),因此它不会在每个循环中导入。

  • You should put your end code inside the while loop .您应该将结束代码放在 while 循环中 Right now you never change the the value keepgoing so the loop will loop forever :现在你永远不变的值keepgoing如此循环将永远循环下去:

  • 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.我尝试尽可能多地保留变量名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM