Python中的石头、布、剪刀游戏的问题
我目前正在用Python编写一个小石头、布、剪刀游戏,但我似乎遇到了一个问题。虽然代码有点粗糙,但游戏仍然可以运行,但我尝试让程序通知玩家他们犯了错误,当我测试出来时,它会随机通知玩家他们犯了错误,而他们没有。这是我的问题代码块,它不是整个游戏Python中的石头、布、剪刀游戏的问题,python,Python,我目前正在用Python编写一个小石头、布、剪刀游戏,但我似乎遇到了一个问题。虽然代码有点粗糙,但游戏仍然可以运行,但我尝试让程序通知玩家他们犯了错误,当我测试出来时,它会随机通知玩家他们犯了错误,而他们没有。这是我的问题代码块,它不是整个游戏 def game(self): print "This is rock, paper, scissors!" rps = ('rock', 'paper', 'scissors') comp1 = raw_input("Ro
def game(self):
print "This is rock, paper, scissors!"
rps = ('rock', 'paper', 'scissors')
comp1 = raw_input("Rock, paper or scissors?\n> ")
comp2 = random.choice(rps)
if comp1 == 'rock' and comp2 == 'scissors' or comp1 == 'scissors' and comp2 == 'paper' or comp1 == 'paper' and comp2 == 'rock':
print "You won! The computer chose %s" % comp2
return "game"
elif comp1 == 'rock' and comp2 == 'rock':
print "In a stalemate, there are no winners, only losers :)\nThe computer also chose %s" % comp2
return "game"
elif comp1 == 'scissors' and comp2 == 'scissors':
print "In a stalemate, there are no winners, only losers :)\nThe computer also chose %s" % comp2
return "game"
elif comp1 == 'paper' and comp2 == 'paper':
print "In a stalemate, there are no winners, only losers :)\nThe computer also chose %s" % comp2
return "game"
elif comp1 != 'rock' or 'scissors' or 'paper':
print "Try choosing between rock, paper and scissors next time. It might help.
return "game"
else:
print "The computer %s. You have failed. Problem?" % comp2
return "game"
改变
到
您之前所做的工作相当于:
elif (comp1 != 'rock') or 'scissors' or 'paper':
那么,为什么总是满足这些条件呢
仔细观察“剪刀”或“布”部分
1在Python中,非空字符串被视为True,空字符串被视为False。请看一下此交互式会话:
>>> bool('')
False
>>> bool('a')
True
2同样在Python中,没有比较的if语句,例如if var1:正在检查表达式是否为真。所以
if var1:
与
if var1 == True:
如果您将这两个想法结合在一起:
if 'rock':
# Always executed
if '':
# Never executed
回到原始的if语句:
elif comp1 != 'rock' or 'scissors' or 'paper':
“剪刀”和“纸”都将始终返回True,因此将始终对包含的语句进行评估
那么in操作符是什么呢
elif comp1 not in rps:中的in运算符将查看comp1的内容是否是元组rps中的一项,该项等于“岩石”、“布”、“剪刀”。前面的not将否定它,检查comp1的内容是否是元组rps中的项。因此,仅当comp1中存储的用户输入无效时,才会执行包含的语句。应该是这样的
comp1 not in ['rock', 'scissors', 'paper']
“剪刀”和“布”的评估结果总是正确的,或者是正确的,或者是正确的
comp1 != 'rock' or 'scissors' or 'paper'
另外,使用comp1==comp2,它要简单得多。这里的问题在于您的逻辑elif comp1!='“石头”或“剪刀”或“布”:。字符串“剪刀”和“纸”将被计算为布尔值,这是真的,因为它们不为null
你想要的是elif comp1!='摇滚乐和comp1!='剪刀和comp1!='纸张“:或者,由于您已经在rps元组中有了它,您可以在rps中执行elif comp1:我认为这是一个更干净的版本,尽管不是最好的实现。我还添加了一个选项来继续播放,并对其进行了更改,使用户输入不区分大小写
def game():
import random
import string
print "This is rock, paper, scissors!"
rps = ('rock', 'paper', 'scissors')
comp1 = raw_input("Rock, paper or scissors? ")
comp1 = comp1.lower()
comp2 = random.choice(rps)
if comp1 == 'rock' and comp2 == 'scissors' or comp1 == 'scissors' and comp2 == 'paper' or comp1 == 'paper' and comp2 == 'rock':
print "You won! The computer chose %s" % comp2
elif comp1 == comp2:
print "In a stalemate, there are no winners, only losers :)\nThe computer also chose %s" % comp2
elif comp1 not in rps:
print "Try choosing between rock, paper and scissors next time. It might help."
else:
print "The computer chose %s. You have failed. Problem?" % comp2
new_game = raw_input('Would you like to play again? ')
new_game = new_game.lower()
if new_game == 'yes':
game()
在我的手机上,但尝试将comp1转换为小写。你也可以通过comp1==comp2来简化逻辑,因为僵局你没有复制真正的代码,因为你复制的是无效的字符串文字。为什么你总是返回字符串游戏,这看起来很奇怪。。。可能与你的问题无关。使用更简单的世界,它会更简单P
comp1 != 'rock' or 'scissors' or 'paper'
def game():
import random
import string
print "This is rock, paper, scissors!"
rps = ('rock', 'paper', 'scissors')
comp1 = raw_input("Rock, paper or scissors? ")
comp1 = comp1.lower()
comp2 = random.choice(rps)
if comp1 == 'rock' and comp2 == 'scissors' or comp1 == 'scissors' and comp2 == 'paper' or comp1 == 'paper' and comp2 == 'rock':
print "You won! The computer chose %s" % comp2
elif comp1 == comp2:
print "In a stalemate, there are no winners, only losers :)\nThe computer also chose %s" % comp2
elif comp1 not in rps:
print "Try choosing between rock, paper and scissors next time. It might help."
else:
print "The computer chose %s. You have failed. Problem?" % comp2
new_game = raw_input('Would you like to play again? ')
new_game = new_game.lower()
if new_game == 'yes':
game()