我做了一个让我玩石头剪刀的剧本。我无法打电话给班级。为什么呢?
import random
player_choice = input("Choose rock paper or scissors")
class rock_paper_scissors:
def __init__(self, player_choice, moves, ai_choice):
self.player_choice
self.moves = ["rock", "paper", "scissors"]
ai_choice = random.choice(moves)
for player_choice in rock_paper_scissors:
if player_choice == "rock" and ai_choice == "scissors":
print("You win Rock beat's scissors")
elif ai_choice == "paper":
print("You lose! Paper beats Rock")
if player_choice == "paper" and ai_choice == "rock":
print("You win! Paper beats rock!")
elif ai_choice == "Scissors":
print("You lose! Scissors beats paper!")
if player_choice == scissors and ai_choice == paper:
print("You win")
elif ai_choice == rock:
print("You lose! Rocky beats paper!")
if player_choice == ai_choice:
print("You tied!")
if player_choice == scissors and ai_choice == paper:
print("You win")
elif ai_choice == rock:
print("You lose! Rocky beats paper!")
if player_choice == ai_choice:
print("You tied!")
错误:
NameError:名称'rock_paper_scissors'未在main.py
的第8行定义
答案 0 :(得分:1)
我认为解释需要解决的问题更有帮助:
self.player_choice
未定义; ai_choice
传递给构造函数,然后在其中明确覆盖其值; moves
传递给您的构造函数。rock_paper_scissors
进行for循环,因为你还没有告诉Python如何迭代一个类; elif
代替if
,这令人困惑; scissors
,rock
,paper
被称为变量,而不是用引号括起来制作字符串。这些参考文献都没有定义。 这是你应该如何修复它,风格更好:
import random
class Game(object): # it's considered good practice to inherit from `object`
# this is a constructor where we declare all instance variables
def __init__(self):
# create a dictionary of what defeats what; accessible in all member
# functions - I deliberately chose to do this to reduce the number
# of if statements in your code and improve readability.
self.victory_map = {"rock":"scissors", "paper":"rock", "scissors":"paper"}
# a method to play only a single round
def play(self):
# Here I ask player's input, and make a random choice from only our
# victory_map's keys
player_choice = input("Type 'rock', 'paper' or 'scissors' exactly")
ai_choice = random.choice(self.victory_map.keys())
# handle case where user types in something not in self.moves
while player_choice not in self.victory_map:
print("Type one of 'rock', 'paper' or 'scissors' EXACTLY as written!")
player_choice = input("Type 'rock', 'paper' or 'scissors'")
# This block takes care of the actual victory logic
if self.victory_map[player_choice] == ai_choice:
print("You win!")
elif self.victory_map[ai_choice] == player_choice:
print("You lose")
else:
print("It's a draw.")
if __name__ == '__main__':
new_game = Game()
while True:
# infinite loop, causes you to play indefinitely
new_game.play()
答案 1 :(得分:0)
我们的代码中发生了许多奇怪的事情。首先,您要创建一个无意义的rock_paper_scissors对象实例。这不是创建对象有益的情况。在这样的例子中,管理你的rps对象比它的价值更麻烦。其次,你忘了用引号包装很多字符串。
我将ai_choice == rock:
之类的内容更改为ai_choice == "rock":
并删除了RPS对象的使用,但它确实有效。
有一点需要注意的是,所有这些if语句都不是判断谁是赢家的好方法。创建一系列win条件并引用它。
import random
player_choice = input("Choose rock paper or scissors ")
moves = ["rock", "paper", "scissors"]
ai_choice = random.choice(moves)
if player_choice == "rock" and ai_choice == "scissors":
print("You win Rock beat's scissors")
elif ai_choice == "paper":
print("You lose! Paper beats Rock")
if player_choice == "paper" and ai_choice == "rock":
print("You win! Paper beats rock!")
elif ai_choice == "Scissors":
print("You lose! Scissors beats paper!")
if player_choice == "scissors" and ai_choice == "paper":
print("You win")
elif ai_choice == "rock":
print("You lose! Rocky beats paper!")
if player_choice == ai_choice:
print("You tied!")
if player_choice == "scissors" and ai_choice == "paper":
print("You win")
elif ai_choice == "rock":
print("You lose! Rocky beats paper!")
if player_choice == ai_choice:
print("You tied!")
答案 2 :(得分:0)
你走了:
import random
# Ask user for info
player_input = input("Choose rock paper or scissors: ")
# Main class
class rock_paper_scissors:
# Initialiser method
def __init__(self, player):
self.player_choice = player
self.moves = ["rock", "paper", "scissors"]
self.ai_choice = random.choice(self.moves)
# Method to check who won
def checkWhoWon(self):
if self.player_choice == "rock" and self.ai_choice == "scissors":
print("You win Rock beat's scissors")
elif self.ai_choice == "paper":
print("You lose! Paper beats Rock")
if self.player_choice == "paper" and self.ai_choice == "rock":
print("You win! Paper beats rock!")
elif self.ai_choice == "Scissors":
print("You lose! Scissors beats paper!")
if self.player_choice == "scissors" and self.ai_choice == "paper":
print("You win")
elif self.ai_choice == "rock":
print("You lose! Rocky beats paper!")
if self.player_choice == self.ai_choice:
print("You tied!")
if self.player_choice == "scissors" and self.ai_choice == "paper":
print("You win")
elif self.ai_choice == "rock":
print("You lose! Rocky beats paper!")
if self.player_choice == self.ai_choice:
print("You tied!")
# Create an object called main from the class "rock_paper_scissors"
Main = rock_paper_scissors(player_input)
# Call the method to check who won
Main.checkWhoWon()