Home > Net >  while loop cannot run
while loop cannot run

Time:11-11

how do i loop back this data. this is part of a bigger code. after this statement ends i will also need to input another str to loop back into a bigger while loop.

specificReport = "yes"

while specificReport == "yes":
    specificReport = str.lower(input("Do you want to look at the specific report (yes/no) : " ))
    option2 = input("Which data are you looking for (1. Location Name, 2.Risk Type, 3. Risk Level , 4. Date & Time): ")
    if int(option2) == 1:
        locationValue = input("Enter the location you want to inspect: ")   
        print(df.loc[df['Location Name'] == locationValue])
    elif int(option2) == 2:
        riskTypeValue = str.lower(input("Enter the risk type you want to inspect: "))
        print(df.loc[df['Risk Type'] == riskTypeValue])
    elif int(option2) == 3:
        riskLevelValue = str.lower(input("Enter the location you want to inspect: "))   
        print(df.loc[df['Risk Level'] == riskLevelValue])                    
    elif int(option2) == 4:
        dAndtValue = input("Enter the location you want to inspect: ")   
        print(df.loc[df['Date & Time'] == dAndtValue])                    
    else:
        print("You have keyed in an invalid option")

CodePudding user response:

You have to update the specificReport. For example like this:

specificReport = "yes"

while specificReport == "yes":
    specificReport = str.lower(input("Do you want to look at the specific report (yes/no) : " ))
    option2 = input("Which data are you looking for (1. Location Name, 2.Risk Type, 3. Risk Level , 4. Date & Time): ")
    if int(option2) == 1:
        locationValue = input("Enter the location you want to inspect: ")   
        print(df.loc[df['Location Name'] == locationValue])
    elif int(option2) == 2:
        riskTypeValue = str.lower(input("Enter the risk type you want to inspect: "))
        print(df.loc[df['Risk Type'] == riskTypeValue])
    elif int(option2) == 3:
        riskLevelValue = str.lower(input("Enter the location you want to inspect: "))   
        print(df.loc[df['Risk Level'] == riskLevelValue])                    
    elif int(option2) == 4:
        dAndtValue = input("Enter the location you want to inspect: ")   
        print(df.loc[df['Date & Time'] == dAndtValue])                    
    else:
        print("You have keyed in an invalid option")
        specificReport = "no"

CodePudding user response:

If you want to loop one more time, you will have to set specificReport == "yes" at the end of your while loop.

Because you are changing it here (at the first line of the loop):

specificReport = str.lower(input("Do you want to look at the specific report (yes/no) : " ))

And not returning it to "yes" at the end.

May I suggest using while True: instead and if condition: break when you reach a condition that you want it to stop looping, this way you dont have to use string comparison specificReport == "yes" to define your loop progression.

while True:
    specificReport = str.lower(input("Do you want to look at the specific report (yes/no) : " ))
    option2 = input("Which data are you looking for (1. Location Name, 2.Risk Type, 3. Risk Level , 4. Date & Time, 5. Exit): ")
    if int(option2) == 1:
        locationValue = input("Enter the location you want to inspect: ")   
        print(df.loc[df['Location Name'] == locationValue])
    elif int(option2) == 2:
        riskTypeValue = str.lower(input("Enter the risk type you want to inspect: "))
        print(df.loc[df['Risk Type'] == riskTypeValue])
    elif int(option2) == 3:
        riskLevelValue = str.lower(input("Enter the location you want to inspect: "))   
        print(df.loc[df['Risk Level'] == riskLevelValue])                    
    elif int(option2) == 4:
        dAndtValue = input("Enter the location you want to inspect: ")   
        print(df.loc[df['Date & Time'] == dAndtValue])
    elif int(option2) == 5:
        print("Exiting")
        break
    else:
        print("You have keyed in an invalid option")
    
  • Related