Python“;石头、布、剪刀”;验证
我一直在做一个石头剪刀游戏,它就像一个梦。然而,当我尝试在中添加一些验证时(用Python“;石头、布、剪刀”;验证,python,Python,我一直在做一个石头剪刀游戏,它就像一个梦。然而,当我尝试在中添加一些验证时(用#显示),我的游戏无法运行。我不知道为什么会这样 我的代码如下: from random import randint from sys import exit computer = randint(1,3) r = "r" p = "p" s = "s" print ("The computer has chosen. Your turn") play
#
显示),我的游戏无法运行。我不知道为什么会这样
我的代码如下:
from random import randint
from sys import exit
computer = randint(1,3)
r = "r"
p = "p"
s = "s"
print ("The computer has chosen. Your turn")
player = input ("r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ")
#from here
if (player != r or p or s):
player = input ("That wasn't r, p, or s. Please try again. r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ")
if (player != r or p or s) :
print ("Can you srsly not understand that " + player + " is not r, p, or s? I give up")
exit()
#to here
if (computer == 1):
AI = ("rock")
if (computer == 2):
AI = ("paper")
if (computer == 3):
AI = ("scissors")
if (player == r and computer == 1):
print ("lol draw")
exit()
if (player == p and computer == 2):
print ("lol draw")
exit()
if (player == s and computer == 3):
print ("lol draw")
exit()
if (player == r and computer == 3):
print ("You WIN!!!!!! AI chose " + AI)
if (player == p and computer == 1):
print ("You WIN!!!!!! AI chose " + AI)
if (player == s and computer == 2):
print ("You WIN!!!!!! AI chose " + AI)
if (player == s and computer == 1):
print ("You LOSE!!!!!! AI chose " + AI)
if (player == r and computer == 2):
print ("You LOSE!!!!!! AI chose " + AI)
if (player == p and computer == 3):
print ("You LOSE!!!!!! AI chose " + AI)
改变这个
if (player != r or p or s):
对此
if player != r and player != p and player != s:
改变这个
if (player != r or p or s):
对此
if player != r and player != p and player != s:
请重新启动
或
运算符
player != r or p or s
应该是
player not in (r, p, s)
或类似的
说明:
如果认为A
为真(真),则A或B
计算为A
。如果A
被认为是错误的(例如False
,0
,0.0
,[]
,'
),则A或B
的计算结果为B
player!=r或p或s
与(玩家!=r)或p或s
相同。现在(player!=r)或p或s的计算结果为True
如果player!=r
和top
否则。由于True
和p
都是“True”,因此这两行是等效的:
if player != r or p or s:
if True:
请重新启动或
运算符
player != r or p or s
应该是
player not in (r, p, s)
或类似的
说明:
如果认为A
为真(真),则A或B
计算为A
。如果A
被认为是错误的(例如False
,0
,0.0
,[]
,'
),则A或B
的计算结果为B
player!=r或p或s
与(玩家!=r)或p或s
相同。现在(player!=r)或p或s的计算结果为True
如果player!=r
和top
否则。由于True
和p
都是“True”,因此这两行是等效的:
if player != r or p or s:
if True:
您拥有的等价于(不是真正的Python代码):
if player!=r或if p或if s
这不是您想要的:因为p
和s
已经定义为true,所以每次都会触发if语句
你想要如果玩家!=r和玩家!=p和玩家!=s
(即,这三件事都不是真的)
如果播放器不在[r,p,s]
中,如果您愿意,它也可以同等工作——我认为它更像Python。您拥有的是等效的(不是真正的Python代码):
if player!=r或if p或if s
这不是您想要的:因为p
和s
已经定义为true,所以每次都会触发if语句
你想要如果玩家!=r和玩家!=p和玩家!=s
(即,这三件事都不是真的)
如果播放器不在[r,p,s]
中,如果您愿意,它也可以同等工作——我认为这有点像python。这不是语法问题的答案,但是我觉得按照前面介绍的类似结构,“石头,布,剪刀”验证可以大大简化
# read player input and return "r", "p" or "s"
def getPlayerChoice():
# return value read from input, as per your code, with the fixes.
# read computer choice and return "r", "p" or "s"
def getComputerChoice():
# return "r", "p", or "s" based on the random number
# get the "English" name of a value
def getName(rps):
# given "r", "p", or "s", return the name like "rock", etc.
# now these two variables will have the value "r", "p" or "s"
# no numbers, no "rock", just some consistent values
player = getPlayerChoice()
computer = getComputerChoice()
# By using consistent values we are able to make the tie check very easy
# and make the win checks easy to read.
# chose same value - "r", "p" or "s"
if player == computer:
print("Tie")
# all ways player could have won
elif ( (player == "r" and computer == "s")
or (player == "p" and computer == "r")
or (player == "s" and computer == "p")):
print("You win! " + getName(player) + " beats " + getName(computer))
# otherwise the computer won
else:
print("You lose! " + getName(computer) + " beats " + getName(player))
YMMV,以及上述结构中的任何缺陷都是“免费的”。这不是语法问题的答案,但是我觉得,遵循类似的结构,可以大大清理“石头、布、剪刀”验证
# read player input and return "r", "p" or "s"
def getPlayerChoice():
# return value read from input, as per your code, with the fixes.
# read computer choice and return "r", "p" or "s"
def getComputerChoice():
# return "r", "p", or "s" based on the random number
# get the "English" name of a value
def getName(rps):
# given "r", "p", or "s", return the name like "rock", etc.
# now these two variables will have the value "r", "p" or "s"
# no numbers, no "rock", just some consistent values
player = getPlayerChoice()
computer = getComputerChoice()
# By using consistent values we are able to make the tie check very easy
# and make the win checks easy to read.
# chose same value - "r", "p" or "s"
if player == computer:
print("Tie")
# all ways player could have won
elif ( (player == "r" and computer == "s")
or (player == "p" and computer == "r")
or (player == "s" and computer == "p")):
print("You win! " + getName(player) + " beats " + getName(computer))
# otherwise the computer won
else:
print("You lose! " + getName(computer) + " beats " + getName(player))
以上结构中的任何错误都是“免费的”。以下是使用一些更高级python习惯用法的代码的简短版本:
from random import randint
from sys import exit
computer = randint(0,2)
choices = 'rps'
print ("The computer has chosen. Your turn")
player = raw_input("r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ")
if (player not in choices):
player = raw_input("That wasn't r, p, or s. Please try again. r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ")
if (player not in choices):
print ("Can you srsly not understand that '%s' is not r, p, or s? I give up" % player)
exit()
if (player == choices[computer]):
print ("lol draw, AI also chose %s" % choices[computer])
exit()
flip = choices.index(player) > computer
result = ("WIN", "LOSE")[(flip + choices.index(player) - computer) % 2]
print ("You %s!!!!!! AI chose %s" % (result, choices[computer]))
下面是一个简短的代码版本,使用了一些更高级的python习惯用法:
from random import randint
from sys import exit
computer = randint(0,2)
choices = 'rps'
print ("The computer has chosen. Your turn")
player = raw_input("r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ")
if (player not in choices):
player = raw_input("That wasn't r, p, or s. Please try again. r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ")
if (player not in choices):
print ("Can you srsly not understand that '%s' is not r, p, or s? I give up" % player)
exit()
if (player == choices[computer]):
print ("lol draw, AI also chose %s" % choices[computer])
exit()
flip = choices.index(player) > computer
result = ("WIN", "LOSE")[(flip + choices.index(player) - computer) % 2]
print ("You %s!!!!!! AI chose %s" % (result, choices[computer]))
你说“不行”是什么意思?现在把“石头、布、剪刀、蜥蜴、斯波克”编码为。。。看来我的问题已经解决了。TYRemember记得在正确的问题旁边打勾。我想你的意思是回答“它不起作用”是什么意思?现在代码是“石头、布、剪刀、蜥蜴、斯波克”。。。看来我的问题已经解决了。TYRemember请记住在正确的问题旁边打勾。我想你的意思是回答解释为什么,因为我猜你是新手:你所拥有的等同于(不是真正的Python代码):这个答案不正确x!=真或x!=False
将始终产生True
…应该是和而不是或来解释原因,因为我猜你是新手:你所拥有的等同于(不是真正的Python代码):这个答案不正确x!=真或x!=False
将始终产生True
…应该是和而不是或乐于帮助,请阅读我的解释并尝试消化它。并阅读python逻辑运算符的文档。很高兴能提供帮助,请阅读我的解释并尝试理解它。并阅读python逻辑运算符的文档。不是根据我的repl<代码>玩家='r'p='p's='s'如果(玩家!=r或p或s):如果(玩家!=r)打印'yes':打印'no'
给出yes.OK,player!=r或p或s
为真
iffplayer!=r
,否则为p
。所以基本上它总是真的
。不用担心——实际上我的代码中有一个完全不同的错误,我已经修复了。介意取消你的否决票吗?他们让我很伤心。我一离开CD就会。不是根据我的回复<代码>玩家='r'p='p's='s'如果(玩家!=r或p或s):如果(玩家!=r)打印'yes':打印'no'
给出yes.OK,player!=r或p或s
为真
iffplayer!=r
,否则为p
。所以基本上它总是真的
。不用担心——实际上我的代码中有一个完全不同的错误,我已经修复了。介意取消你的否决票吗?他们让我很难过。我一离开CD就去。对不起,我必须投反对票,因为这是无效的python。@SethMMorton,没关系,绝对不是。我确实纠正了&&/| |/|的严重误用;和条件表达式。愚蠢的催促了我。对不起,我必须投反对票,因为这是无效的。@SethMMorton好的,绝对不是。我确实纠正了&&/| |/|的严重误用;和c