简体   繁体   中英

Issues With Restarting a While Loop In Python

I am trying to restart a While loop but am having issues. I've tried formatting the code several ways, and IDLE always returns with ">>>"

responses = {}

polling_active = True

while polling_active:
    name = input("\nWhat is your name? ")
    response = input("Which mountain would you like to climb someday? ")

    responses[name] = response

    repeat = input("Would you like to let another person respond? (yes/no) ")
    if repeat == 'no':
       polling_active = False

print("\n--- Poll Results ---")
for name, response in responses.items():
    print(f"{name} would like to climb {response}.")

    response = input("\nWould you like to start a new poll? (yes/no) ")
    if response == 'yes':
        polling_active = True
        continue
    if response == 'no': 
       print("Thank you, have a good day!")
       continue

Put your while loop in a seperate function like this:

def poll():
  while polling_active:
      name = input("\nWhat is your name? ")
      response = input("Which mountain would you like to climb someday? ")

      responses[name] = response

      repeat = input("Would you like to let another person respond? (yes/no) ")
      if repeat == 'no':
         polling_active = False

Then, instead of calling continue at the end, simply run this function.

Python is an interpreter language. The code runs from top to bottom. once it exits your while loop. it cannot return. Just use the function as mentioned in above answer.

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