Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

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. It only prints the else statement.

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 
"))
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! ")  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you want cpu = randint(0,2) so that it's just the int. Then later when you need the string, replace cpu with t[cpu]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...