boo voorhees
boo voorhees

Reputation: 13

NameError, variable not defined in Python

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

Upvotes: 0

Views: 102

Answers (3)

Sri Harsha K M
Sri Harsha K M

Reputation: 31

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.

Upvotes: 0

Shaiful Islam
Shaiful Islam

Reputation: 379

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

Upvotes: 1

Red
Red

Reputation: 27577

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.

Upvotes: 0

Related Questions