Need help adding a loop to restart program in Python

后端 未结 3 1525
臣服心动
臣服心动 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 09:01

    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.

提交回复
热议问题