【发布时间】: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