【问题标题】:I can't seem to get out of the loop on this program on python我似乎无法摆脱这个程序在 python 上的循环
【发布时间】:2017-02-08 01:12:48
【问题描述】:

我的第一堂编程课大约有 5 周时间,这对我来说仍然有点困难。我想知道是否有人可以帮助我。

我想我似乎无法弄清楚我在做什么错,在完成 7 天的输入命令后,它又回到了第一天。 这是我第一次在这里发帖,所以如果我输入了几乎所有的代码,我深表歉意,我只是为了参考,看看它是否可能在 while 循环之上或之下导致我的程序重复自己。 提前感谢您的帮助!

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

【问题讨论】:

  • 以上代码不可能为您运行而没有错误。请确保您的要求与您的实际问题相符。在你这样做之后:你需要跳出封闭的 while 循环,而你似乎从来没有这样做过。
  • 将您的第一个 While 更改为 while。你能正确缩进你的程序吗?
  • While keepgoing == "y": 不是有效的 Python(注意大写字母),循环体没有缩进。
  • 您是否试图获得无限循环?因为这就是你获得无限循环的方式
  • 问题是您实际发布的代码根本不会运行,但您似乎在询问运行的代码,但不是您期望的那样。因此 - 您发布的代码不是您正在运行的代码。请编辑您的问题,以便它显示您的实际代码。由于问题几乎可以肯定是缩进之一,因此您将代码复制到 Stack Overflow 的方式已经破坏了缩进这一事实使您的问题无法回答。删除这个不成功的副本,再次粘贴您的代码,选择它,然后点击{} 图标。

标签: python loops while-loop


【解决方案1】:

我只是简单地修改了您的代码并重新格式化了一点:

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

请注意:

  • 变量的命名不好,用sunday_salessundaySales好,sales_listSales等好。 Naming Conventions
  • 在你计算Saturday sales的末尾的原始代码中有一个return True,实际上它应该是break。 (顺便说一句,while True 确实是一种糟糕的做法,请尽可能避免使用它)Why while(true) is bad practice?
  • 代码缩进不正确
  • 变量不一致:keepgoing vs keepGoing
  • 当您输入Do you want to run this again? (Enter 'y')=时,请确保您输入的字符串带有引号"abc"'abc',而不是abc,否则会出现错误,因为Python将输入视为raw_input
  • 正如@Lord of dark 所说,循环导入不好,请在文件或函数定义的开头导入(如果有循环导入) Python ImportingCircular (or cyclic) imports in Python

【讨论】:

  • 谢谢你,这些是一些很棒的技巧,我真诚地感谢大家的帮助!
【解决方案2】:

以下是一些改进代码的建议:

  • 不要在函数外使用return True。您只能使用return 离开函数。要离开 while 循环,请使用 break

  • 在程序开头导入包(将 import totalOutcome 移到顶部),因此不会在每个循环中都导入。

  • 您应该将结束代码放在 while 循环中。现在你永远不会改变值 keepgoing 所以循环将永远循环:

  • 您不应该为每一天手动编写代码,您应该遍历一个日期列表并将每个结果存储在一个列表中。

这是此代码的更紧凑版本:

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

我尝试尽可能地保留变量名称。

【讨论】:

    猜你喜欢
    • 2018-01-14
    • 1970-01-01
    • 2014-03-28
    • 2020-01-31
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 2013-06-20
    相关资源
    最近更新 更多