我在我的第一个编程课程中大约有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!")
答案 0 :(得分: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_sales
而不是sundaySales
,sales_list
比Sales
等更好。
Naming Conventions return True
时,您的原始代码中有Saturday sales
,实际上它应该是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
答案 1 :(得分:1)
以下是一些改进代码的建议:
请勿在函数外使用return True
。您只能使用return
来保留功能。要使用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
我尝试过尽量保留变量名称。