Python循环程序

时间:2017-04-28 16:37:02

标签: python loops

所以我创造了一个Rock,Paper,Lizard,Spock游戏。我似乎无法弄清楚如何对它进行循环,所以我想要类似的东西 raw_input(“如果你想再玩一次,请输入(Y或N)”)我也想知道如何输出得分?谢谢。

以下是代码:

import random
comp = random.randrange(5)+1

user = raw_input("Choose:\nRock\nPaper\nScissors\nLizard\nSpock\n")

if comp == 1:
    if user == "rock":
        print "Computer chooses ROCK, its a TIE."
    elif user == "rock" or "spock":
        print "Computer chooses ROCK, computer WINS."
    elif user == "sissors" or "lizard":
        print "Computer chooses ROCK, you WIN."
elif comp == 2:
    if user == "paper":
        print "Computer chooses PAPER, its a TIE."
    elif user == "lizard" or "sissors":
        print "Computer chooses PAPER, computer WINS."
    elif user == "rock" or "spock":
        print "Computer chooses PAPER, you WIN"
elif comp == 3:
    if user == "sissors":
        print "Computer chooses SISSORS, its a TIE."
    elif user == "spock" or "rock":
        print "Computer chooses SISSORS, computer WINS."
    elif user == "lizard" or "paper":
        print "Computer chooses SISSORS, you WIN."
elif comp == 4:
    if user == "lizard":
        print "Computer chooses LIZARD, its a TIE."
    elif user == "rock" or "spock":
        print "Computer chooses LIZARD, computer WINS."
    elif user == "sissors" or "lizard":
        print "Computer chooses LIZARD, you WIN."
elif comp == 5:
    if user == "spock":
        print "Computer chooses SPOCK, its a TIE."
    elif user == "paper" or "lizard":
        print "Computer chooses SPOCK, computer WINS."
    elif user == "rock" or "sissors":
        print "Computer chooses SPOCK, you WIN"
else:
    print "ERROR"

1 个答案:

答案 0 :(得分:0)

(Python2):

import random

while True:

    comp = random.randrange(5)+1

    user = raw_input("Choose:\nRock\nPaper\nScissors\nLizard\nSpock\n")

    if comp == 1:
        if user == "rock":
            print "Computer chooses ROCK, its a TIE."
        elif user == "rock" or "spock":
            print "Computer chooses ROCK, computer WINS."
        elif user == "sissors" or "lizard":
            print "Computer chooses ROCK, you WIN."
    elif comp == 2:
        if user == "paper":
            print "Computer chooses PAPER, its a TIE."
        elif user == "lizard" or "sissors":
            print "Computer chooses PAPER, computer WINS."
        elif user == "rock" or "spock":
            print "Computer chooses PAPER, you WIN"
    elif comp == 3:
        if user == "sissors":
            print "Computer chooses SISSORS, its a TIE."
        elif user == "spock" or "rock":
            print "Computer chooses SISSORS, computer WINS."
        elif user == "lizard" or "paper":
            print "Computer chooses SISSORS, you WIN."
    elif comp == 4:
        if user == "lizard":
            print "Computer chooses LIZARD, its a TIE."
        elif user == "rock" or "spock":
            print "Computer chooses LIZARD, computer WINS."
        elif user == "sissors" or "lizard":
            print "Computer chooses LIZARD, you WIN."
    elif comp == 5:
        if user == "spock":
            print "Computer chooses SPOCK, its a TIE."
        elif user == "paper" or "lizard":
            print "Computer chooses SPOCK, computer WINS."
        elif user == "rock" or "sissors":
            print "Computer chooses SPOCK, you WIN"
    else:
        print "ERROR"

    continue_input = raw_input('Enter (Y or N) if you would like to play again')
    if continue_input.lower() != 'y':
        break