岩石剪刀蜥蜴Spock差异结果不起作用

时间:2015-08-21 04:50:37

标签: python difference

一切都很好,但差异变量。我试过寻找答案,但它没有用,我需要帮助。

import random



selection = {0 : "Rock", 1 : "Paper", 2 : "Scissors", 3 : "Lizard", 4 : "Spock"}


Playerwins = 0
Computerwins = 0


def RPSLS():

definition RPSLS()

    global Playerwins, Computerwins

    Player = random.randrange(5)
    Computer = random.randrange(5)

    print "Player chooses", selection[Player]
    print "Computer chooses", selection[Computer]

    Difference = (Player - Computer) % 5



    if Difference == 0:
        print "Player and Computer tie!"
    elif Difference <= 2:
        Playerwins += 1
        print "Player Wins!"
    else:
        Computerwins += 1
        print "Computer Wins!"

    print "Player Wins:", Playerwins
    print "Computer Wins:", Computerwins
    print "\n"

def main():
    while Computerwins or Playerwins != 10:

        raw_input("\nPress the enter key to start a game.")
        print "\n"
        RPSLS()
        if Computerwins or Playerwins == 10:
            if Computerwins == 10:
                print "Sorry you lost, Computer wins!"
                break
            elif Playerwins == 10:
                print "Congratulations you won!"
                break
main()

# Allowing the user to exit the program

raw_input("\n\nPress the enter key to exit.")

1 个答案:

答案 0 :(得分:0)

你的字典错了,正确的答案是正确的顺序 -

selection = {0 : "Rock", 1 : "Spock", 2 : "Paper", 3 : "Lizard", 4 : "Scissors"}

此外,您的代码中存在其他问题 -

while Computerwins or Playerwins != 10:

这是错误的,这不是你如何检查其中任何一个值是否达到10,它将检查Computerwins是否为0或空字符串/空列表等(在这种情况下它表示为True)或者玩家已经没有达到10,所以这个while循环退出的唯一条件是当计算机验证为0且玩家必须达到10时。应该是 -

 while Computerwins != 10 or Playerwins != 10:

与if条件类似的问题 -

 if Computerwins or Playerwins == 10:

应该是 -

if Computerwins == 10 or Playerwins == 10:

目前,部分代码正常工作只是因为上面提到的if..elif中的if块,但在修复了while循环之后,完整的if部分可以移到而声明如 -

def main():
    while Computerwins != 10 or Playerwins != 10:
        raw_input("\nPress the enter key to start a game.")
        print "\n"
        RPSLS()

    if Computerwins == 10:
        print "Sorry you lost, Computer wins!"
    elif Playerwins == 10:
        print "Congratulations you won!"