Python石头、剪刀、布,else/elif语句的问题



我想做一个石头剪子布游戏,但我觉得我在else/elif语句上做错了。

import random
list = ['R', 'P', 'S']
def play():
x = input('Please input R, P, or S: ').upper()
y = random.choice(list)
if x == y:
print('You both picked {}, pick again'.format(x))
play()
elif (x == 'r' and y == 's') or (x == 'p' and y == 'r') or (x == 's' and y == 'p'):
print('You won! You picked {} and computer picked {}'.format(x,y))
else:
print('You lost. You picked {} and computer picked {}'.format(x,y))
play()

您的测试中list'r', 'p', 's'中有'R', 'P', 'S'。因此,==在您的if中总是被评估为False

另一件事:你应该避免调用变量list。这是python内置的类型。

最后的评论(归功于@JoshuaVoskamp),你的代码不处理答案不在['R', 'S', 'P']中的情况。

所以这是一个正确的解决方案,总结了不同的评论:

import random
choices = ['R', 'P', 'S']
def play():
x = input('Please input R, P, or S: ').upper()
y = random.choice(choices)
if x == y:
print('You both picked {}, pick again'.format(x))
play()
elif x + y in ['RS', 'PR', 'SP']:  # clever test proposed by @user17242583
print('You won! You picked {} and computer picked {}'.format(x,y))
elif x in ['R', 'S', 'P']:
print('You lost. You picked {} and computer picked {}'.format(x,y))
else:
print('Wrong input!')
play()
play()

Try This

import tkinter as tk
from PIL import Image,ImageTk

window=tk.Tk()
window.geometry("300x500")
window.title("Rock Paper Scissors")
image=Image.open('rps.jpg')
image.thumbnail((300,300),Image.ANTIALIAS)
photo=ImageTk.PhotoImage(image)
label_image=tk.Label(image=photo)
label_image.grid(column=15,row=0)
#global variables
USER_SCORE=0
COMP_SCORE=0
USER_CHOICE=""
COMP_CHOICE=""

def choice_to_number(choice):
rps={'scissor':0,'paper':1,'rock':2}
return rps[choice]
def number_to_choice(number):
rps={0:'scissor',1:'paper',2:'rock'}
return rps[number]

def random_computer_choice():
return random.choice(['scissor','paper','rock'])
def result(human_choice,comp_choice):
global USER_SCORE
global COMP_SCORE
user=choice_to_number(human_choice)
comp=choice_to_number(comp_choice)
if(user==comp):
print("Tie")
elif((user-comp)%3==1):
print("Sorry !! Computer win")
USER_SCORE+=1
else:
print("Congarts !! You win")
COMP_SCORE+=1

#Text
text_area=tk.Text(master=window,height=12,width=30)
text_area.grid(column=15,row=4)
answer="Your Choice: {uc} nComputer's Choice : {cc} n Your Score : {u} n Computer Score : {c}  nn made by diwas pandey ".format(uc=USER_CHOICE,cc=COMP_CHOICE,u=USER_SCORE,c=COMP_SCORE, font=('arial',24,'bold'))
text_area.insert(tk.END,answer)

#Event Handling
def rock():
global USER_CHOICE
global COMP_CHOICE

USER_CHOICE='rock'
COMP_CHOICE=random_computer_choice()
result(USER_CHOICE,COMP_CHOICE)
def paper():
global USER_CHOICE
global COMP_CHOICE

USER_CHOICE='paper'
COMP_CHOICE=random_computer_choice()
result(USER_CHOICE,COMP_CHOICE)
def scissor():
global USER_CHOICE
global COMP_CHOICE

USER_CHOICE='scissor'
COMP_CHOICE=random_computer_choice()
result(USER_CHOICE,COMP_CHOICE)

#buttons
button1=tk.Button(text="       Scissor         ",bg="red",command=scissor, height=1,width=8,font=('arial',15,'bold'))
button1.grid(column=15,row=1)
button2=tk.Button(text="        Paper          ",bg="pink",command=paper, height=1,width=8,font=('arial',15,'bold'))
button2.grid(column=15,row=2)
button3=tk.Button(text="         Rock          ",bg="yellow",command=rock, height=1,width=8,font=('arial',15,'bold'))
button3.grid(column=15,row=3)  

window.mainloop()```

对于input使用upper()方法,因此elif语句中的字母应该全部大写,如:

import random
computer = ['R', 'P', 'S']
def play():
x = input('Please input R, P, or S: ').upper()
y = random.choice(computer)
if x == y:
print('You both picked {}, pick again'.format(x))
play()
elif (x == 'R' and y == 'S') or (x == 'P' and y == 'R') or (x == 'S' and y == 'P'):
print('You won! You picked {} and computer picked {}'.format(x,y))
else:
print('You lost. You picked {} and computer picked {}'.format(x,y))
play()

相关内容

  • 没有找到相关文章

最新更新