Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python while循环逻辑_Python_While Loop - Fatal编程技术网

Python while循环逻辑

Python while循环逻辑,python,while-loop,Python,While Loop,当我在pythonshell上运行我的程序时。它对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 = [

当我在pythonshell上运行我的程序时。它对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.

简而言之:您既有无限循环,也有不正确的递归

不正确的递归:

其他字母启动另一个
decisions
函数调用,它有自己的循环

换句话说,
decisions()
(实例1)-->
totals()
在字母“T”之后-->
decisions()
(实例2)-->等等

当输入字母“E”时,只有最新的
决策
循环结束。前面的循环将继续(在本例中为无限循环)

无限循环:

此外,由于您在进入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.

原因是所有函数都以再次运行函数决策结束。因此,从main开始,然后运行决策,然后选择选项T来运行函数总计。因为函数总计以decisions()结束您现在有两个决策循环在运行。因此,当您按E键打破该循环时,您只会打破最近的决策循环,它会使您返回到前一个循环。摆脱所有这些决策()在所有函数结束时,它将开始工作得更好。你不需要告诉程序返回到描述。当你完成在决策中调用的函数时,它将自动返回到循环中。作为旁注,更多的注释比更少的注释更好,但我同意前面的海报,这有点过头了评论很糟糕。

旁注:这是一个荒谬的过度评论的案例。@StevenCumming你的教授在评论的情况下似乎有点强迫症。或者你误解了,有点过火了吗?@StevenCumming你的教授似乎不明白变量命名的意义。上次我因为评论太少而被打了半票,所以我被解雇了我是一个聪明的助手,变量
标志
没有任何作用。只要使用
,而使用True