这是我的代码,由于某种原因,游戏的结果未显示,我也不知道为什么,任何人都可以解释我如何执行所需的结果。我已经尝试弄乱我的代码一段时间了,如果您知道如何解决此问题,可以教给我,这样下次我就不会再犯同样的错误了。谢谢。
import random
Rock= 0
Paper = 1
Scissors = 2
Quest = input("Rock, Paper, Scissors?")
print("You choose",Quest)
AI = random.randint(0,2)
#What the A.I Chooses
if AI == 0:
print("A.I Chooses Rock")
if AI == 1:
print("A.I Chooses Paper")
if AI == 2:
print("A.I Chooses Scissors")
# if you draw:
if AI == 0 and Quest == Rock:
print("So, you draw")
if AI == 1 and Quest == Paper:
print("So, you draw")
if AI == 2 and Quest == Scissors:
print("So, you draw")
# Rock Possible Outcomes
if AI == 0 and Quest == Paper:
print("So, you loose")
if AI == 0 and Quest == Scissors:
print("So, you win")
# paper Possible Outcomes
if AI == 1 and Quest == Rock:
print("So, you loose")
if AI == 1 and Quest == Scissors:
print("So, you win")
#Scissors Possible Outcomes
if AI == 2 and Quest == Paper:
print("So, you loose")
if AI == 2 and Quest == Rock:
print("So, you win")
答案 0 :(得分:1)
如果您输入与您的选择有关的单词,则应注意它们将不被视为相等:
>>> if 0 == 'rock': print('match')
# no output
如果您输入与您的选择有关的数字,它们仍将被视为一个字符串(只是一个不同的字符串),并且不会被视为相等:
>>> if 0 == '0': print('match')
# no output
您需要做的是确保将数字与数字进行比较:
>>> if 0 == int('0'): print('match')
match
您可以通过确保输入数字y字符串 并将其上一个数字进行比较来做到这一点:
Quest = int(input("0 = rock, 1 = paper, 2 = scissors?"))
您可以也可以只使用字符串,前提是您要与"rock"
(字符串)而不是数字rock
进行比较。但是,如果您看下面的代码,您会发现数字可以使您的代码小得多。
如果您想要更简洁的实现,则可以考虑以下一种方法:
import random
random.seed()
choices = ['rock', 'paper', 'scissors']
# Make sure user enters valid choice.
choice = ''
while choice not in choices:
choice = input('Rock, paper, or scissors? ').lower()
print('You chose', choice)
you = choices.index(choice)
# AI just makes random choice.
ai = random.randint(0,2)
print('AI chose', choices[ai])
# Draw if both same, otherwise three scenarios in which player wins,
# otherwise AI wins.
if you == ai:
print('A draw')
elif (you == 0 and ai == 2) or (you == 1 and ai == 0) or (you == 2 and ai == 1):
print('You win')
else:
print('AI wins')
它可以将所有内容尽快汇总,以确保比较是一件简单的事情。
答案 1 :(得分:0)