[英]else & elif statements seems that is not working in Python
I'm traying to make a simple rock paper scissors game but it seems that the if
and elif
statement do not respond.我正在制作一个简单的石头剪刀布游戏,但似乎if
和elif
语句没有响应。 It only print
s the else
statement.它只print
else
语句。
Here's the code:这是代码:
from random import randint
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
t = [rock, paper, scissors]
player = int(input("choose 0 for rock and 1 for paper and 2 for scissors \n"))
cpu = t[randint(0,2)]
print(t[player])
print(cpu)
if player == 0 and cpu == 2:
print("YOU WIN")
elif cpu == 0 and player == 2:
print("YOU LOSt")
elif player == 1 and cpu == 0:
print("you win")
elif cpu == 1 and player == 0:
print(" YOU LOST")
else:
print("this is invalid input! ")
I think you want cpu = randint(0,2)
so that it's just the int.我认为您想要cpu = randint(0,2)
以便它只是 int。 Then later when you need the string, replace cpu
with t[cpu]
然后稍后当您需要字符串时,将cpu
替换为t[cpu]
change this line cpu = t[randint(0,2)]
to this cpu = randint(0,2)
将此行cpu = t[randint(0,2)]
更改为此cpu = randint(0,2)
As others mentioned, you need to change the two lines to:正如其他人提到的,您需要将这两行更改为:
cpu = randint(0,2)
print(t[player])
print(t[cpu])
By the way, there is a simple way to determine the outcome without if-statements:顺便说一句,有一种简单的方法可以在没有 if 语句的情况下确定结果:
from random import randint
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
outcome = ["DRAW", "YOU LOST", "YOU WON"]
t = [rock, paper, scissors]
player = int(input("choose 0 for rock and 1 for paper and 2 for scissors \n"))
cpu = randint(0, 2)
result = (cpu - player) % 3
print(t[player])
print(t[cpu])
print(outcome[result])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.