简体   繁体   中英

How to exit a while-loop with the value of an input which is inside another while-loop?

loop1 = True
while loop1 == True:
info = input("option 1,2 or 3?")
if info == '1':
    print("while 1, option 1")
    loop2 = True
    while loop2 == True:
        again = input("1- again(while2) or 2- Close(while2)")
        if again == '1':
            info2 = input("info 2-")
        elif again == '2':
            print("Close!!!")
            loop2 == False
        else:
            print("ERRO! again")
            again = input("again? 2-")

elif info == '2':
    print("while 1, option 2")
elif info == '3':
    print("while 1, option3, Exit!")
    loop1 = False      
else:
    print("erro!")
    info = input("err! Can you tell me how much is the ")


print("I'm out!")

I'm new in python! My goal is to get out of loop 2 with the value of info2 and enter loop1 with the value of info2 so that you can choose another option such as option 2 or 3. I know I have to loop1 = loop2, but I do not know where to put the code

you will need to begin by fixing your indentation... cant exit a sentinel while loop when the code to do so is outside both while loops. Id also recommend setting "universal" variables to manipulate each of the while loops, (when these variables overlap then you should be able to exit one when using another. Try using something other than boolean statements to declare each sentinal loops ( even simple strings would do better). If this is exercising your ability in using while loops than keep going with this method, but really python offers a lot easier ways to create this program.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM