Python Forum
Rock, Paper, Scissors Game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rock, Paper, Scissors Game
#1
Hi everyone,
I am new to python and I am creating a rock, paper, scissors game. I do not get an error in my code, however, when I run it and input rock, paper or scissors as my option, it returns "none". I've research everywhere and I am completely stuck and I really want to understand what I am doing wrong.

Below is my python code:

# Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input),
# compare them, print out a message of congratulations to the winner, and ask if the
# players want to start a new game)
# Rock beats scissors
# Scissors beats paper
# Paper beats rock

print("Welcome to Rock, Paper, Scissors game: This is a two-player game. Player 1 and Player 2 will choose rock, "
      "paper, or scissors respectively\n")

p_1 = input("Player 1, choose rock, paper, or scissors: ")
p_2 = input("Player 2, choose rock, paper, or scissors: ")

def play (player_1, player_2):
    if player_1 == player_2:
        return play_again()

    elif player_1 == 'rock'.lower():
        if player_2 == 'scissors'.lower():
            return "Rock hits scissors. Player 1 win!"

    elif player_1 == 'paper'.lower():
        if player_2 == 'scissors'.lower():
            return "Scissors cut paper. Player 2 wins"

    elif player_1 == 'scissors'.lower():
        if player_2 == 'rock'.lower():
            return "Rock hits scissors. Player 2 wins!"

    elif player_1 == 'rock'.lower():
        if player_2 == 'paper'.lower():
            return "Paper covers rock. Player 2 wins!"

    elif player_1 == 'scissors'.lower():
        if player_2 == 'paper'.lower():
            return "Scissors cut paper. Player 1 wins!"

    elif player_1 == 'paper'.lower():
        if player_2 == 'rock'.lower():
            return "Paper covers rock. Player 1 wins!"
        else:
            return "Paper wins"
    else:
        print("Invalid choice. Type either, type rock, paper, or scissors.")
        return play(p_1, p_2)

print(play(p_1, p_2))

def play_again ():
    if input("Try again?") == "yes".lower():
        return play(p_1, p_2)
Reply
#2
Try this to see if it helps,

items=['paper','scissors','rock']# items list
i=True
while i==True:
	#read strings , convert lower
	p_1=input("\nenter player 1 choice: ").lower()
	p_2=input("enter player 2 choice: ").lower()
	if (p_1 != p_2) and (p_1 in items) and (p_2 in items):# verify not same and given input are in list-items
		if (p_1=="rock" and p_2=="scissors") or (p_1=="paper" and p_2=="rock") or (p_1=="scissors" and p_2=="paper"):#player 1 win criteria
			print(f"{p_1} beats {p_2} , player 1 win!")
		else:
			print(f"{p_2} beats {p_1} , player 2 win!")
		#play again? yes/not
		playagain=input("play again: (yes/no) : " ).lower()
		if playagain == "yes":
			i=True
		else:
			i=False # stop while loop
	else:
		print(f"bad input/same words, {p_1,p_2} retrying game ")
		pass 
Output:
python test1.py enter player 1 choice: ROck enter player 2 choice: PAPER paper beats rock , player 2 win! play again: (yes/no) : yes enter player 1 choice: rock enter player 2 choice: scissors rock beats scissors , player 1 win! play again: (yes/no) : yes enter player 1 choice: paper enter player 2 choice: SCISSORS scissors beats paper , player 2 win! play again: (yes/no) : yes enter player 1 choice: paper enter player 2 choice: rock paper beats rock , player 1 win! play again: (yes/no) : yes enter player 1 choice: scissors enter player 2 choice: paper scissors beats paper , player 1 win! play again: (yes/no) : yes enter player 1 choice: scissors enter player 2 choice: rock rock beats scissors , player 2 win! play again: (yes/no) : yes enter player 1 choice: rock enter player 2 choice: ROCK bad input/same words, ('rock', 'rock') retrying game enter player 1 choice: rrr enter player 2 choice: ttt bad input/same words, ('rrr', 'ttt') retrying game enter player 1 choice: rock enter player 2 choice: paper paper beats rock , player 2 win! play again: (yes/no) : no
Few observations on your code,
#lower() to be applied for the input taken variables, not on right hand side of comparing text.
(eg: player_1.lower() == 'paper':)

#None, because, assume rock, paper given, it first match main elif block, then it only have if block, it fail compare to scissors and thus looks for else or elif inside the main elif , found nothing and closing main elif and returning None with statemtnt print(play(p_1, p_2)) as none of if/elif/else got match return though elif block sucessful with p_!, but exited as no catch of p_2 string, i guess.

#tryagain code, you took input yes but no block/function defined to repeat taking inputs for re-run.

#elif conditions were looking little complex, may be you can try like,

elif player_1.lower() == 'rock':
		if player_2.lower() == 'scissors':
			return "Rock hits scissors. Player 1 win!"
		elif player_2.lower() == 'paper':
			return "Paper covers rock. Player 2 wins!"
			
elif player_1.lower() == 'paper':
		if player_2.lower() == 'scissors':
			return "Scissors cut paper. Player 2 wins"
		elif player_2.lower() == 'rock':
			return "Paper covers rock. Player 1 wins!"
			
elif player_1.lower() == 'scissors':
		if player_2.lower() == 'rock':
			return "Rock hits scissors. Player 2 wins!"
		elif player_2.lower() == 'paper':
			return "Scissors cut paper. Player 1 wins!"
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
Reply
#3
Another option is to rethink problem before writing any code.

There are three winning combinations (first beats second):

first_wins = [('rock', 'scissors'), ('paper', 'rock'), ('scissors', 'paper')]
All other combinations are either draw or second wins.

If we will ask players their choice then we can construct tuple (or list) from their answers and compare whether answers are equal (draw) or first wins. If neither then second is winner.

It can expressed like below assumes that user input is converted lowercase. In order to get correct answer from this snippet one must have user input validation (only allowed choices can be entered) otherwise second player 'wins' (not equal, not in first wins combination):

if player_one == player_two:
    # draw
elif (player_one, player_two) in first_wins:
    # first player wins
else:
    # second player wins
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I attempted to make a rock paper scissors bot for homework Intellectual11 3 2,885 Jun-24-2021, 08:00 PM
Last Post: deanhystad
Question Rock, paper, scissors spelling error banidjamali 6 3,162 Jan-19-2021, 02:51 PM
Last Post: banidjamali
  I need help with a python Rock Paper Scissors game urgently. Riff_Raff 3 5,815 Dec-05-2018, 09:13 PM
Last Post: nilamo
  Rock, Paper, Scissors Advanced that saves, loads, and keeps statistics EvanCahill 0 5,195 Jul-21-2018, 07:32 PM
Last Post: EvanCahill
  Rock Paper Scissors Warmlawpk441 4 5,010 Oct-11-2017, 10:55 AM
Last Post: gruntfutuk
  Rock paper scissors game samiraheen 3 6,336 Oct-03-2017, 07:07 PM
Last Post: buran
  The Python Book altered rock paper scissors Python_Noob 0 2,903 Sep-18-2017, 06:13 AM
Last Post: Python_Noob
  HELP---problems with rock paper scissors games kalt91 2 4,109 Sep-15-2017, 04:51 PM
Last Post: micseydel
  Rock, Paper, Scissors game help.. hentera 3 5,026 May-19-2017, 10:56 PM
Last Post: ichabod801
  Rock Paper Scissors game codeobri 3 13,384 Apr-28-2017, 01:02 AM
Last Post: codeobri

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020