首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"Play“没有定义- Pylance (reportUndefinedVariable)

"Play“没有定义- Pylance (reportUndefinedVariable)
EN

Stack Overflow用户
提问于 2020-11-25 19:22:08
回答 1查看 24.8K关注 0票数 5

我刚刚开始学习Python,在调用类时遇到了困难。我在使用代码。我查过错误,但找不到任何有用的东西。到目前为止,我所有的经验都是关于Java的,并且认为我可能搞混了一些东西。任何帮助都将不胜感激!

代码语言:javascript
复制
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!")

以上是供今后参考的原始问题。以下是解决办法。Python与Java不同,因为在引用类之前,必须先定义它们。这意味着类必须位于python文件的顶部。希望这能帮助其他Java书呆子尝试用Python编写程序..。

代码语言:javascript
复制
    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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-25 20:16:27

Python是从上到下执行的,因此您的所有类和finctions都应该在调用之前定义(所以放在顶部)。也是

代码语言:javascript
复制
class Play:
    def __init__(player1, player2):
        self.player1 = player1
        self.player2 = player2

您应该像这样在类中定义属性,在此之前,self引用类的当前实例,这里中的所有内容。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65011428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档