Python 石头、布、剪刀游戏
我试图用Python完成我的第一个“游戏”,但它就是不起作用,我找不到错误。。。 这是我的密码:Python 石头、布、剪刀游戏,python,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":
#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"
我每次都会出错,有时甚至会得到一个错误的“偶数”答案。。。
我认为错误在决定谁赢的部分…我怀疑在你比较用户选择和计算机选择的块中,你是想使用“and”而不是“&”。这是按位and所做的,你似乎不明白:
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,除了位。使用关键字
和
我找不到错误
,你认为我们可以吗?你没有告诉我们错误是什么或者你想做什么?你知道和和&
之间的区别吗?第二个是,它不做你认为它做的事情。我想问一下,需要将&
更改为和在Python中做什么?Python不使用符号逻辑运算符,所以&&什么都不做。我的错。我一直在ruby中工作,虽然两者都存在,但优先级不同。我已编辑了答案以更正错误。