lov*_*ish 7 python visual-studio-code
我刚刚开始学习 Python,并且在调用类时遇到问题。我正在使用 Visual Studio 代码。我已经查找了错误,但找不到任何有用的信息。到目前为止,我所有的经验都是关于 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!")
Run Code Online (Sandbox Code Playgroud)
以上是原问题,供以后参考。下面是解决方案。Python 与 Java 的不同之处在于,必须在引用类之前定义类。这意味着这些类必须位于 python 文件的顶部。希望这可以帮助其他一些尝试用 Python 编写程序的 Java 书呆子......

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()
Run Code Online (Sandbox Code Playgroud)
Python是从上到下执行的,所以所有的类和函数都应该在调用之前定义(所以放在顶部)。还
class Play:
def __init__(player1, player2):
self.player1 = player1
self.player2 = player2
Run Code Online (Sandbox Code Playgroud)
您应该像这样在类中定义属性,self它指的是类的当前实例,这里的所有内容。
| 归档时间: |
|
| 查看次数: |
40363 次 |
| 最近记录: |