Need help adding a loop to restart program in Python

后端 未结 3 1520
臣服心动
臣服心动 2021-01-28 08:36

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         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-28 08:58

    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
    

提交回复
热议问题