"Play" is not defined - Pylance (reportUndefinedVariable)

Viewed 21220

I am just starting Python and am having a problem with calling classes. I am using Visual Studio Code, might be the problem? I have looked up the error but could not find anything. I have all of my experience with Java and think I might be getting some stuff mixed up. Any help would be greatly appreciated!

print("Lets play a game!")

dotheywantotplay = input("Do you want to play Rock-Paper-Scissors? (y or n) ")

if dotheywantotplay == "y":

    player1 = input('Enter first players name: ')
    player2 = input('Enter second players name: ')

    personchoice1 = input("What move will you do? [\"Rock\", \"Paper\", \"Scissors\"]")

    personchoice2 = input("What move will you do? [\"Rock\", \"Paper\", \"Scissors\"]")

    Play(personchoice1,personchoice2) // The error looks like its this line

else:
    print("I am so sad now :-(")
    exit()


class Play:
    def __init__(player1, player2):

        if player1 == player2:
                print("Tie!")
        elif player1 == "Rock":
            if player2 == "Paper":
                print("You lose!", player2, "covers", player1)
            else:
                print("You win!", player1, "smashes", player2)
        elif player1 == "Paper":
            if player2 == "Scissors":
                print("You lose!", player2, "cut", player1)
            else:
                print("You win!", player1, "covers", player2)
        elif player1 == "Scissors":
            if player2 == "Rock":
                print("You lose...", player2, "smashes", player1)
            else:
                print("You win!", player1, "cut", player2)
        else:
            print("That's not a valid play. Check your spelling!")

Above was the original question for future reference. Below is the solution. Python is different that Java in that, one has to define classes before they are referenced. This means the classes need to be at the top of your python file. Hope this helps some other Java nerd out there trying to write programs in Python... Program working

    print("Lets play a game!")
    
    dotheywantotplay = input("Do you want to play Rock-Paper-Scissors? (y or n) ")
    




print("Lets play a game!")

dotheywantotplay = input("Do you want to play Rock-Paper-Scissors? (y or n) ")



class Play:
    def __init__(self, player1, player2, name1, name2 ):

        self.player1 = player1
        self.player2 = player2
        self.name1 = name1
        self.name2 = name2


        if player1 == player2:
                print("Tie!")
        elif player1 == "Rock":
            if player2 == "Paper":
                print("You Win ", name2,"!")
                print(player2, "covers", player1)
            else:
                print("You Win ", name1,"!")
                print(player1, "smashes", player2)
        elif player1 == "Paper":
            if player2 == "Scissors":
                print("You Win ", name2,"!")
                print(player2, "cut", player1)
            else:
                print("You Win ", name1,"!")
                print(player1, "covers", player2)
        elif player1 == "Scissors":
            if player2 == "Rock":
                print("You Win ", name2,"!")
                print(player2, "smashes", player1)
            else:
                print("You Win ", name1,"!")
                print(player1, "cut", player2)
        else:
            print("That's not a valid play. Check your spelling!")


if dotheywantotplay == "y":

    player1 = input('Enter first players name: ')
    player2 = input('Enter second players name: ')

    personchoice1 = input("What move will you do? \"Rock\", \"Paper\", \"Scissors\"  ")
    personchoice2 = input("What move will you do? \"Rock\", \"Paper\", \"Scissors\"  ")

    Play(personchoice1,personchoice2, player1, player2)

else:
    print("I am sad now :-(")
    exit()
1 Answers

Python is executed top to bottom, so all your Classes and finctions should be defined before called (so placed on top). Also

class Play:
    def __init__(player1, player2):
        self.player1 = player1
        self.player2 = player2

you should define your attributes inside your class like this before anything else, self it refers to the current instance of your class, everything in here.

Related