Need help adding a loop to restart program in Python

痴心易碎 提交于 2019-12-20 05:47:23

问题


So far this is what I have got.

import random

answer1=("Absolutely!")
answer2=("No way Pedro!")
answer3=("Go for it tiger.")
answer4=("There's different ways to do this.")
answer5=("Definitely not")

print("Welcome to the Magic 8 Ball game-use it to answer your questions...")

questio = input("Ask me for any advice and I'll help you out. Type in your question and then press Enter for an answer")

print("Shaking... \n" * 4) 

choice=random.randint(1,5)

if choice == 1:
    answer=answer1
elif choice == 2:
    answer=answer2
elif choice == 3:
      answer=answer3
elif choice == 4:
      answer=answer4

elif choice == 5:
      answer=answer5


print(answer)

restart = input("Would you like to try again?")

if restart == "yes" or "y":

now I need to add a loop to this so after its done it displays "Would you like to try again?". After entering yes the program starts again from the top.


回答1:


Try adding a loop like this to your code:

restart = ''

while restart.upper() not in ['NO', 'N', 'EXIT']:
    # ...  
    # Your code here
    # ...

    restart = input("Would you like to try again?")

This is essentially initializing restart as an empty string. Then the loop is only going to start while restart != 'NO'. At the end of the loop we're getting a new value from the user for restart so that if they say 'no' or 'n' the loop won't start again.

There are lots of other ways to do this, such as the inverse where you could check to see if the user entered a positive answer ('yes' or 'y') or putting a break if they say 'no'.

Hopefully this gets you started.




回答2:


after the import line add while True: indent the entire code to the while loop. then at the end go like this:

    restart = input("Would you like to try again?")

    if restart == "yes":
        continue
    else:
        break

To explain further. Continue will jump back up to the top pass would work as well. break will exit the loop. good luck!




回答3:


The easiest way is to use a while loop like so:

import random

while True:
    answer1=("Absolutely!")
    answer2=("No way Pedro!")
    answer3=("Go for it tiger.")
    answer4=("There's different ways to do this.")
    answer5=("Definitely not")

    print("Welcome to the Magic 8 Ball game-use it to answer your questions...")

    questio = input("Ask me for any advice and I'll help you out. Type in your question and then press Enter for an answer ")

    print("Shaking... \n" * 4) 

    choice=random.randint(1,5)

    if choice == 1:
        answer=answer1
    elif choice == 2:
        answer=answer2
    elif choice == 3:
          answer=answer3
    elif choice == 4:
          answer=answer4

    elif choice == 5:
          answer=answer5

    print(answer)

    restart = input("Would you like to try again? ")

    if restart == "yes" or "y":
        continue

    else:
        break


来源:https://stackoverflow.com/questions/33902905/need-help-adding-a-loop-to-restart-program-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!