0

so the ultimate goal is to run best 2 out of 3 games of rock, paper, scissors, lizard, spock. i haven't added a loop yet or anything like that i'm trying to get the game itself running first but I'm coming across a NameError, it's saying the 'result' variable is undefined.

I've tried returning it but that doesn't appear to be working, but I also maybe don't know what I'm doing?

    
def number_to_name(number):

    if number == 1:
        return 'scissors'
    elif number == 2:
        return 'rock'
    elif number == 3:
        return 'paper'
    elif number == 4:
        return 'lizard'
    elif number == 5:
        return 'spock'
    else:
        print ("Error: Invalid number")

def name_to_number(name):

    if name == 'scissors':
        return 1
    elif name == 'rock':
        return 2
    elif name == 'paper':
        return 3
    elif name == 'lizard':
        return 4
    elif name == 'spock':
        return 5
    else:
        print ("Error: Invalid number")


def rpsls(name):

    player_score, computer_score = (0, 0)
    player_input = name_to_number(name)
    computer_input = random.randint(1,5)
    result = (player_input - computer_input) % 5


if result == 1 or result == 2:
    print("player wins")
    player_score += 1
    print("Player {}, Computer {}". format(player_score, computer_score))
    
elif result == 3 or result == 4:
    game = "computer wins"
    computer_score += 1
    print("Player {}, Computer {}". format(player_score, computer_score))
    
elif result == 0:
    game = "it's a tie"
    print("Player {}, Computer {}". format(player_score, computer_score))
    
else:
    print("error")


        
rpsls("rock")
rpsls("spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")
2
  • Please reduce and enhance this into the expected MRE. You posted 50 lines of code for a 5-line problem. You apparently didn't try to reduce the code to trace the problem. See this lovely reference for debugging help. Commented Jun 25, 2020 at 4:47
  • Your condition is not placed inside the loop..Move to inside the rpsls function Commented Jun 25, 2020 at 4:50

3 Answers 3

1

Your conditions should be inside the rpsls function.Because you result variable is local variable. You can't fetch this variable globally.

> def rpsls(name):
>     player_score, computer_score = (0, 0)
>     player_input = name_to_number(name)
>     computer_input = random.randint(1, 5)
>     result = (player_input - computer_input) % 5
> 
> 
>     if result == 1 or result == 2:
>         print("player wins")
>         player_score += 1
>         print("Player {}, Computer {}".format(player_score, computer_score))
> 
>     elif result == 3 or result == 4:
>         game = "computer wins"
>         computer_score += 1
>         print("Player {}, Computer {}".format(player_score, computer_score))
> 
>     elif result == 0:
>         game = "it's a tie"
>         print("Player {}, Computer {}".format(player_score, computer_score))
> 
>     else:
>         print("error")
Sign up to request clarification or add additional context in comments.

Comments

0

Your variable result is inside the function rpsls. So the scope of result lies to the function only.

A easy solution would be assign a 0 value to result before the function 'rpsls' This way your updating a globally defined variable inside the function.

result = 0
def rpsls(name):
   #Your code

The best way would be to write a class, have a class level variable result, and put all this code into the class.

Comments

0

First of all, since result is only defined in the function, it is only accessable inside that specific function, unless you choose to use the global method, which I wouldn't recommend.

Second, since you called result before you called the function that actually defines result, even if you use global, it will still not be defined for that specific line.

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.