"Play" is not defined – Pylance (reportUndefinedVariable)

Question:

I’m just starting to learn Python and am having trouble calling classes. I’m using Visual Studio Code. I’ve looked up the error but couldn’t find anything helpful. All of my experience so far has been 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 than Java in that, one has to define classes before they are referenced. This means the classes must 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()
Asked By: love2phish

||

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.

Answered By: JLeno46
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.