1

I am trying to create a little Rock paper scissors game as I am a beginner but I am having issues with my if and elif statements.

import random
player_score = 0
computer_score = 0
options = ['rock', 'paper', 'scissors']

def player_choice():
    input('Rock, Paper or Scissors? ')
    return player_choice
def computer_choice():
    print(random.choice(options))
    return computer_choice

ps = print('player score: ', player_score)
cs = print('computer_score: ',computer_score)

while player_score or computer_score < 10:
    player_choice()
    computer_choice()

if player_choice == 'rock' and computer_choice == 'rock':
        print('Tie')
elif player_choice == 'rock' and computer_choice == 'paper':
        print('Computer wins')
        computer_score = computer_score + 1
        print(ps)
        print(cs)
elif player_choice == 'rock' and computer_choice == 'scissors':
        print('You win')
        player_score = player_score + 1
        print(ps)
        print(cs)

It seems as though the entire if/ elif block is ignored and nothing prints or is incremented. No error pops up, it is just simply ignored.

2
  • player_choice = player_choice() computer_choice = computer_choice() and you'll probably want to tab your if statement in so that it lies beneath your while statement Commented Jul 9, 2020 at 13:27
  • 1
    Did you mean to indent the if ... else statements under the while loop? Commented Jul 9, 2020 at 13:36

4 Answers 4

5

There are a couple of problems with your code, and I am going to try and address all of them.

The first one has to do with the naming of your variables. You name your functions computer_choice and player_choice, and then check if they equal "rock" or other strings. This is only going to return False because computer_choice is a function, not a string. I would recommend changing the names of your functions to get_computer_choice() and get_player_choice()

Secondly, ps = print('player score: ', player_score). I don't know what you are trying to do there. ps will be None, because print() doesn't return anything.

Third, your functions return themselves.

def my_func():
    return my_func

Will return a function. What you want to do for both of your choice functions is this:

def get_player_choice():
    player_choice = input('Rock, Paper or Scissors? ')
    return player_choice

def get_computer_choice():
    computer_choice = random.choice(options) # Set computer_choice to computers choice
    print(computer_choice)
    return computer_choice

Fourth, under your while loop, you are calling the functions, but aren't doing anything with the returns. Change

while player_score or computer_score < 10:
    player_choice()
    computer_choice()

to

while player_score or computer_score < 10:
    player_choice = get_player_choice()
    computer_choice = get_computer_choice()

Finally, the if ... else statements need to be indented under the while loop, otherwise they never get executed.

Sign up to request clarification or add additional context in comments.

2 Comments

Good answer, I'll upvote if you add indenting the if statements under the while loop.
@CodingClown lol done. I didn't include it originally because I wasn't sure If that was just a mistake he made when transferring it to StackOverflow.
0

Please check the 13th line of your code. I couldn't understand what ps = print(...) means.

Comments

0

There were several issues with your code.

def player_choice():
    input('Rock, Paper or Scissors? ')
    return player_choice

player_choice is not initialised. Input takes the user input, but the result is not stored anywhere.

def get_player_choice():
    choice = input("Rock, Paper or Scissors? ")
    return choice

Same procedure for computer_choice.

Then there is:

ps = print('player score: ', player_score)
cs = print('computer_score: ',computer_score)

I assume you tried to construct a string containing the scores. I suggest

def print_score():
    print('player score: ', player_score)
    print('computer_score: ',computer_score)

instead. You can then call the function whenever you want to see the score.

And finally, the indent was not right for the if-else statements. It should have been in the while loop. Now it just asks the user for input and generates a choice for the computer in an endless loop.

Here are all changes I suggested combined:


import random
player_score = 0
computer_score = 0
options = ['rock', 'paper', 'scissors']

def get_player_choice():
    player_choice= input('Rock, Paper or Scissors? ')
    return player_choice
def get_computer_choice():
    computer_choice = random.choice(options)
    print(computer_choice)
    return computer_choice

def print_score():
    print('player score: ', player_score)
    print('computer_score: ',computer_score)

while player_score < 10  and computer_score < 10:
    player_choice = get_player_choice()
    computer_choice = get_computer_choice()

    if player_choice == 'rock' and  computer_choice == 'rock':
        print('Tie')
    elif player_choice == 'rock' and computer_choice == 'paper':
        print('Computer wins')
        computer_score = computer_score + 1
        print_score()
    elif player_choice == 'rock' and computer_choice == 'scissors':
        print('You win')
        player_score = player_score + 1
        print_score()

Comments

0

A few notes:

1. I changed the name of your functions to avoid a later error when calling them.

2. As suggested in another answers you must pass a variable to store the result from the functions.

3. It was missing indentation

4. Some of your conditions were not right. Such as the while condition: if ((player_score<10) or (computer_score < 10)), even if someone reaches the score of 10, the game will keep going. We don't want that, so it must be the and condition.

Here is the solution:

import random

player_score = 0
computer_score = 0

options = ['rock', 'paper', 'scissors']

def choice_player():
    
    result_player = str((input('rock, paper or scissors? ')))
    return (result_player)

def choice_computer():
    result_computer = random.choice(options)
    return (result_computer)


#---------------------------------------------
ps = print('player score: ', player_score)
cs = print('computer_score: ',computer_score)

while ((player_score<10) and (computer_score < 10)):
    
    player_choice = choice_player()
    computer_choice = choice_computer()

    if ((player_choice == 'rock') and (computer_choice == 'rock')):
        print('Tie')
    elif ((player_choice == 'rock') and (computer_choice == 'paper')):
        print('Computer wins')
        computer_score = computer_score + 1
        print('player score: ', player_score)
        print('computer_score: ',computer_score)
    elif ((player_choice == 'rock') and (computer_choice == 'scissors')):
        print('You win')
        player_score = player_score + 1
        print('player score: ', player_score)
        print('computer_score: ',computer_score)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.