当我在Python Shell上运行我的程序时。它对于T,D,A,H,L运行正常,但是当我输入T,D,A,H,L之后输入E时,end1()函数运行并且totals()函数也在之后再次运行。如果我重新启动程序并且只输入E,则程序运行正常。
dayArray = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]; #This is the array for the days.
consumedArray = [2600,2400,3500,3200,2700,3300,3000]; #This is the array for the calories consumed.
burnedArray = [150,100,200,180,250,100,50]; #This is the array for the calories burned.
def main(): #This is the main module.
calories = 0 #This is the variable for the calories.
day = "" #This is the variable for the day.
letter = "" #This is the variable for the letter.
totals = 0 #This is the variable for the totals.
difference = 0 #This is the variable for the differences.
average = 0.0 #This is the variable for the average.
max1 = 0 #This is the variable for the max.
min1 = 0 #This is the variable for the min.
flag = False #This is the variable for the flag.
counter = 1 #This is the variable for the counter.
index = 1 #This is the variable for the index.
intro()
decisions()
def intro(): #This is the intro module.
print("Welcome to the program!! Enjoy!!") #This is the message to welcome you to the program.
print("*"*60) #Prints 60 stars.
def decisions(): #This is the decisions module.
print("What do you want to do? ") #This is asking you what you want to do.
print("T = Get Totals") #This is if you type in "T" you will get totals.
print("D = Find Differences") #This is if you type in "D" you will find differences.
print("A = Find Averages") #This is if you type in "A" you will find the averages.
print("H = Find Highest") #This is if you type in "H" you will find the highest.
print("L = Find Lowest") #This is if you type in "L" you will find the lowest.
print('*'*60); #Prints 60 stars.
letter = str(input("What do you want to do? ")) #This is asking you what you want to do.
print("You entered: ", letter) #This is telling you what letter you have entered.
flag=False;
while flag != True: #This is the loop for letter doesnt equals E
if letter == "T": #This is if letter is equal to T.
print("like anything")
totals()
print("like anything")
elif letter == "D": #This is if letter is equal to D.
differences()
elif letter == "A": #This is if letter is equal to A.
averages()
elif letter == "H": #This is if letter is equal to H.
max1()
elif letter == "L": #This is if letter is equal to L.
min1()
elif letter == "E":
break
else:
print("restart and try again") #Anything else end program
break
#when while loop ends end the program
end1()
def differences(): #This is the differences module.
print('*'*60); #Prints 60 stars.
print('Day Calories Consumed Calories Burned'); #This is the output for the column names.
print('*'*60); #Prints 60 stars.
for i in range(7): #This is saying i is in the range of 7.
x = consumedArray[i] - burnedArray[i]; #This is the formula for the differences.
print("{0:10s} {1:12d}" .format(dayArray[i],x)) #This is the output for the days and the differences.
print('*'*60); #Prints 60 stars.
print()
decisions() #run decisions to restart the main content of the program
def totals(): #This is the totals module.
print('*'*60); #Prints 60 stars.
print('Day Calories Consumed Calories Burned'); #This is the output for the column names.
print('*'*60); #Prints 60 stars.
for i in range(7): #This is saying i is in the range of 7.
print("{0:10s} {1:12d} {2:25d}" .format(dayArray[i], consumedArray[i], burnedArray[i])) #This is the output for the totals of days, consumed, and burned.
print('*'*60); #Prints 60 stars.
print()
decisions() #run decisions to restart the main content of the program
def averages(): #This is the averages module.
print('*'*60); #Prints 60 stars.
average1 = sum(consumedArray) / len(consumedArray); #This is the formula for the Average Calories Consumed.
average2 = sum(burnedArray) / len(burnedArray); #This is the formula for th Average Calories Burned.
print("Average Calories Consumed %.2f"% average1) #This is the output for the Average Calories Consumed at 2 decimal places as a float.
print("Average Calories Burned %.2f"% average2) #This is the output for the Average Calories Burned at 2 decimal places as a float.
print('*'*60); #Prints 60 stars.
print()
decisions() #run decisions to restart the main content of the program
def max1(): #This the max1 module.
print('*'*60); #Prints 60 stars.
maxconsumed = max(consumedArray); #This is finding the highest amount of calories consumed.
maxburned = max(burnedArray); #This is finding the highest amount of calories burned.
i = consumedArray.index(maxconsumed); #This is setting the highest amount of calories consumed in the index.
j = burnedArray.index(maxburned); #This is setting the highest amount of calories burned in the index.
print("Highest Calories Consumed {0:10s} {1:2d}" .format(dayArray[i],maxconsumed)) #This is the output for the highest calories consumed.
print("Highest Calories Burned {0:10s} {1:2d}" .format(dayArray[i],maxburned)) #This is the output for the highest calories burned.
print('*'*60); #Prints 60 stars.
print()
decisions() #run decisions to restart the main content of the program
def min1(): #This is the min1 module.
print('*'*60); #Prints 60 stars.
minconsumed = min(consumedArray); #This is finding the lowest amount of calories consumed.
minburned = min(burnedArray); #This is finding the lowest amount of calories burned.
i = consumedArray.index(minconsumed); #This is setting the lowest amount of calories consumed in the index.
j = burnedArray.index(minburned); #This is setting the lowest amount of calories burned in the index.
print("Lowest Calories Consumed {0:10s} {1:2d}" .format(dayArray[i],minconsumed)) #This is the output for the lowest calories consumed.
print("Lowest Calories Burned {0:10s} {1:2d}" .format(dayArray[i],minburned)) #This is the output for the lowest calories burned.
print('*'*60); #Prints 60 stars.
print();
decisions() #run decisions to restart the main content of the program
def end1(): #This is the end module.
print("You have finished the program!!"); #This is telling you that you have finished the program.
main() #This is telling the program to run all of the modules.
答案 0 :(得分:1)
简而言之:你有一个无限循环和不正确的递归。
不正确的递归:
其他字母启动另一个decisions
函数调用,它有自己的循环。
换句话说,decisions()
(实例1) - >字母“T”后的totals()
- > decisions()
(实例2) - >等....
输入字母“E”时,只有最新的decisions
循环结束。之前的循环继续(在这种情况下无限)。
无限循环:
此外,由于在输入while循环之前要求输入字母,所以任何不结束while循环的字母都会导致无限循环。例如,输入“T”将不断重复以下操作:
while flag != True: # flag is never True, by the way
if letter == "T": # this is always True
... # this will be parsed over and over again since it does not break
要修复程序,请删除除主要调用之外的所有decisions()
实例,并将letter = str(input(...
行移至while循环中。
此版本有效,例如:
dayArray = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
"Sunday"] # This is the array for the days.
consumedArray = [2600, 2400, 3500, 3200, 2700, 3300,
3000] # This is the array for the calories consumed.
burnedArray = [150, 100, 200, 180, 250, 100,
50] # This is the array for the calories burned.
def main(): # This is the main module.
calories = 0 #This is the variable for the calories.
day = "" #This is the variable for the day.
letter = "" #This is the variable for the letter.
totals = 0 #This is the variable for the totals.
difference = 0 #This is the variable for the differences.
average = 0.0 #This is the variable for the average.
max1 = 0 #This is the variable for the max.
min1 = 0 #This is the variable for the min.
flag = False #This is the variable for the flag.
counter = 1 #This is the variable for the counter.
index = 1 #This is the variable for the index.
intro()
decisions()
def intro(): # This is the intro module.
print(
"Welcome to the program!! Enjoy!!") #This is the message to welcome you to the program.
print("*" * 60) #Prints 60 stars.
def decisions(): # This is the decisions module.
print("What do you want to do? ") #This is asking you what you want to do.
print("T = Get Totals") #This is if you type in "T" you will get totals.
print("D = Find Differences") #This is if you type in "D" you will find differences.
print("A = Find Averages") #This is if you type in "A" you will find the averages.
print("H = Find Highest") #This is if you type in "H" you will find the highest.
print("L = Find Lowest") #This is if you type in "L" you will find the lowest.
print('*' * 60) #Prints 60 stars.
flag = False
while flag != True: #This is the loop for letter doesnt equals E
letter = str(input("What do you want to do? ")) #This is asking you what you want to do.
print("You entered: ", letter) #This is telling you what letter you have entered.
if letter == "T": #This is if letter is equal to T.
print("like anything")
totals()
print("like anything")
elif letter == "D": #This is if letter is equal to D.
differences()
elif letter == "A": #This is if letter is equal to A.
averages()
elif letter == "H": #This is if letter is equal to H.
max1()
elif letter == "L": #This is if letter is equal to L.
min1()
elif letter == "E":
break
else:
print("restart and try again") #Anything else end program
break
#when while loop ends end the program
end1()
def differences(): # This is the differences module.
print('*' * 60) #Prints 60 stars.
print(
'Day Calories Consumed Calories Burned') #This is the output for the column names.
print('*' * 60) #Prints 60 stars.
for i in range(7): #This is saying i is in the range of 7.
x = consumedArray[i] - burnedArray[i] #This is the formula for the differences.
print("{0:10s} {1:12d}".format(dayArray[i],
x)) #This is the output for the days and the differences.
print('*' * 60) #Prints 60 stars.
print()
# decisions() #run decisions to restart the main content of the program
def totals(): # This is the totals module.
print('*' * 60) #Prints 60 stars.
print(
'Day Calories Consumed Calories Burned') #This is the output for the column names.
print('*' * 60) #Prints 60 stars.
for i in range(7): #This is saying i is in the range of 7.
print("{0:10s} {1:12d} {2:25d}".format(dayArray[i], consumedArray[i], burnedArray[
i])) #This is the output for the totals of days, consumed, and burned.
print('*' * 60) #Prints 60 stars.
print()
# decisions() #run decisions to restart the main content of the program
def averages(): # This is the averages module.
print('*' * 60) #Prints 60 stars.
average1 = sum(consumedArray) / len(
consumedArray) #This is the formula for the Average Calories Consumed.
average2 = sum(burnedArray) / len(
burnedArray) #This is the formula for th Average Calories Burned.
print(
"Average Calories Consumed %.2f" % average1) #This is the output for the Average Calories Consumed at 2 decimal places as a float.
print(
"Average Calories Burned %.2f" % average2) #This is the output for the Average Calories Burned at 2 decimal places as a float.
print('*' * 60) #Prints 60 stars.
print()
# decisions() #run decisions to restart the main content of the program
def max1(): # This the max1 module.
print('*' * 60) #Prints 60 stars.
maxconsumed = max(
consumedArray) #This is finding the highest amount of calories consumed.
maxburned = max(burnedArray) #This is finding the highest amount of calories burned.
i = consumedArray.index(
maxconsumed) #This is setting the highest amount of calories consumed in the index.
j = burnedArray.index(
maxburned) #This is setting the highest amount of calories burned in the index.
print("Highest Calories Consumed {0:10s} {1:2d}".format(dayArray[i],
maxconsumed)) #This is the output for the highest calories consumed.
print("Highest Calories Burned {0:10s} {1:2d}".format(dayArray[i],
maxburned)) #This is the output for the highest calories burned.
print('*' * 60) #Prints 60 stars.
print()
# decisions() #run decisions to restart the main content of the program
def min1(): # This is the min1 module.
print('*' * 60) #Prints 60 stars.
minconsumed = min(
consumedArray) #This is finding the lowest amount of calories consumed.
minburned = min(burnedArray) #This is finding the lowest amount of calories burned.
i = consumedArray.index(
minconsumed) #This is setting the lowest amount of calories consumed in the index.
j = burnedArray.index(
minburned) #This is setting the lowest amount of calories burned in the index.
print("Lowest Calories Consumed {0:10s} {1:2d}".format(dayArray[i],
minconsumed)) #This is the output for the lowest calories consumed.
print("Lowest Calories Burned {0:10s} {1:2d}".format(dayArray[i],
minburned)) #This is the output for the lowest calories burned.
print('*' * 60) #Prints 60 stars.
print()
# decisions() #run decisions to restart the main content of the program
def end1(): # This is the end module.
print(
"You have finished the program!!") #This is telling you that you have finished the program.
main() #This is telling the program to run all of the modules.
答案 1 :(得分:0)
原因是因为所有功能都是通过再次运行功能决策而结束的。所以你从main开始,然后运行决策,然后说你选择选项T来运行函数总计。因为函数总数以decision()结束,所以现在有两个决策循环在运行。因此,当您按E来打破循环时,您只会打破最近的决策循环,它会让您重新回到上一个循环中。在所有功能结束时摆脱所有这些决定(),它将开始更好地工作。你不需要告诉程序回到descisions。当您完成在决策中调用的函数时,它将自动退回到循环中。作为旁注,更多评论更好然后更少,但我同意前一张海报,这对评论有点过分。