Home > Software design >  Python rock, paper, scissors, problem with else/elif statements
Python rock, paper, scissors, problem with else/elif statements

Time:10-26

I am trying to make a rock, paper, scissors game, but I think I am doing something wrong with the else/elif statements.

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()

CodePudding user response:

You have 'R', 'P', 'S' in list and 'r', 'p', 's' in your test. So == tests in your if will always be evaluated to False.

Another thing: you should avoid calling a variable list. That's a python built-in type.

Last comment (credits to @JoshuaVoskamp), your code does not handle the case where the answer is not in ['R', 'S', 'P'].

So here is a correct solution summing up the different comments made:

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()

CodePudding user response:

You use upper() method for the input, therefore the letter in the elif statement should be all uppercase, like:

import random
list = ['R', 'P', 'S']

def play():
    x = input('Please input R, P, or S: ').upper()
    y = list = ['R', 'P', 'S']
    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()

CodePudding user response:

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}  \n\n 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()```
  • Related