gpt4 book ai didi

python - "Play"未定义 - Pylance (reportUndefinedVariable)

转载 作者:行者123 更新时间:2023-12-04 08:31:17 27 4
gpt4 key购买 nike

我刚刚开始使用 Python,并且在调用类时遇到问题。我正在使用 Visual Studio Code,可能是问题所在?我已查找错误,但找不到任何内容。我有我所有的 Java 经验,我认为我可能会混淆一些东西。任何帮助将不胜感激!

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 文件的顶部。希望这可以帮助其他一些尝试用 Python 编写程序的 Java Nerd ......
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()

最佳答案

Python 是从上到下执行的,所以你所有的类和功能都应该在调用之前定义(所以放在顶部)。还

class Play:
def __init__(player1, player2):
self.player1 = player1
self.player2 = player2
你应该先像这样在你的类中定义你的属性, self它指的是你的类的当前实例,在 here 中的所有内容.

关于python - "Play"未定义 - Pylance (reportUndefinedVariable),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65011428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com