我一直在做一个石头剪刀游戏,它的工作就像一个梦。然而,当我试图在#中添加一些验证时,我的游戏就不起作用了,我不知道为什么会这样。
我的代码如下:
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)发布于 2013-12-05 19:35:21
再次使用or操作符。
player != r or p or s应该是
player not in (r, p, s)或者类似的。
解释:
A or B计算为A,如果A被认为是真的(真)。如果A被认为是虚假的(如False、0、0.0、[]、''),则A or B评估为B。
player != r or p or s和(player != r) or p or s是一样的。现在,(player != r) or p or s的计算结果为True,如果是player != r,则为p。由于True和p都是“真”,所以这两行是等价的:
if player != r or p or s:
if True:发布于 2013-12-05 21:02:25
下面是使用一些更高级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]))发布于 2013-12-05 19:35:11
改变这个
if (player != r or p or s):到这个
if player != r and player != p and player != s:https://stackoverflow.com/questions/20408890
复制相似问题