摇滚,纸,剪刀游戏

时间:2015-03-30 22:09:02

标签: python

我试图用Python完成我的第一个“游戏”,但它不会工作,我无法找到错误...... 继承我的代码:

#Rock, Paper, Scissors
import random

userchoice = raw_input("Choose Rock paper or Scissors: ")
print userchoice
uch = 0
cch = 0
cont = 0
winner = 4

cch = random.randint(1,3)

if userchoice == "rock":
    uch = 1
    cont = 1
elif userchoice == "paper":
    uch = 2
    cont = 1
elif userchoice == "scissors":
    uch = 3
    cont = 1
else:
    print "Error"


print uch
print cch
#if cont == 1:

if   uch == 1 & cch == 1:
     winner = 0
elif uch == 1 & cch == 2:
     winner = 2
elif uch == 1 & cch == 3:
     winner = 1
elif uch == 2 & cch == 1:
     winner = 1
elif uch == 2 & cch == 2:
     winner = 0
elif uch == 2 & cch == 3:
    winner = 2
elif uch == 3 & cch == 1:
    winner = 2
elif uch == 3 & cch == 2:
     winner = 1
elif uch == 3 & cch == 3:
    winner = 0
else:
    print "Error"


if winner == 0:
    print "Even"
elif winner == 1:
    print "You win"
elif winner == 2:
    print "Computer wins"
elif winner == 4:
    print "Error2"

我每次都会收到错误,有时错误的“偶数”答案...... 我认为错误是在它决定谁赢的部分的某个地方......

2 个答案:

答案 0 :(得分:1)

这就是按位和做的,因为你似乎不明白:

 integer | binary representation |
-----------------------------------
    5    |          101          |  # bits are aligned and kept in place if
    7    |          111          |  # bits are ones, then then to base 10
-----------------------------------
    5               101

它像逻辑一样,除了比特之外。使用关键字and

答案 1 :(得分:0)

我怀疑在您比较用户选择和计算机选择的区块中,您打算使用"和"而不是"&"。