python repeat program while true [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-31 04:03:25

问题


I am attempting to make my program repeat when the user inputs y/n, however I am confused on how to use a while true with this type of input, below is some code.

again = input("Would you like to play again? enter y/n:  ")
if again == "n":
    print ("Thanks for Playing!")
    quit

if again == "y":
    print ("Lets play again..")
    ????

Also, I want to do an else statement if the user enters in a different character, but am unsure on how to go about that considering I have 2 different if statements.


回答1:


When you're writing a standalone Python program, it’s a good practice to use a main function. it allows you to easily add some unit tests, use your functions or classes from other modules (if you import them), etc.

If you have to check if some condition is satisfied in case some other condition is not satisfied, and perform some actions depending on which condition is true, you can use an if…elif…else statement.

Also, please note that you cannot use the input() function for your program in this case. What you really want to use here is raw_input. The difference between these two functions is that raw_input() will always return a string and input() will evaluate user’s input as if it was written in your code instead of input(). So, if the user enters "y" (with the quotation marks), then a string object is stored as the value for the variable. But if the user enters y (without the quotation marks), input() will try to evaluate this and an error will be thrown if y is not defined.

You can read more on this subject here.

def main():
    while True:
        again = raw_input("Would you like to play again? Enter y/n: ")

        if again == "n":
            print ("Thanks for Playing!")
            return
        elif again == "y":
            print ("Lets play again..")
        else:
            print ("You should enter either \"y\" or \"n\".")

if __name__ == "__main__":
    main()



回答2:


def play_game():
    if int(raw_input("Guess a number:"))!= 5:
          print "You Lose!"
    else:
          print "You Win!"

def play_again():
    return raw_input("Play Again?").lower() == "y"

while True:
    play_game()
    if not play_again(): break

print "OK Goodbye..."



回答3:


You could do something like this:

Assign a bool value to a variable called playing, and then use that as the loop condition.

So you would have;

playing = True
while playing:
    choice = input("would you like to play again? y/n: ")
    if choice == "n":
        print "Thanks for playing"
        playing = False
    else:
        print "play again.. etc..."

Setting the playing variable to false with cause the loop to terminate.




回答4:


I got my code to work and it's looping everytime it goes to else statement, basically loops back to the if statement..

Just started to learn python and I'm really liking it. here is my simple code.

print 'Welcome to "Guess my number"'

def main():
    while True:
        number = raw_input('Please Enter a number between 1 and 10: ')
        if number == '5':
            print 'You Got It!! It\'s number ' + number
            return
        else:
            print 'Please try again!'
main()
raw_input("\nPress enter")


来源:https://stackoverflow.com/questions/12557376/python-repeat-program-while-true

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