类/函数故障



我正在尝试制作一个游戏,让两名玩家轮流猜测我的随机数。我希望电脑能让玩家继续猜,直到猜到正确的数字。一旦发生这种情况,我想让电脑打印出分数。现在,当你运行我的代码时,它会在每个玩家转一圈后打印出分数。我还想知道如何让电脑显示是哪个玩家打开的。任何建议都将不胜感激,因为我对类和对象都是新手。我想明确一点,一旦比赛进行了三轮,比赛就结束了。提前谢谢!

import random
class Players (object): 
def __init__(self, name):
self.name = name
self.gamesWon = 0
self.counter = 0
def __str__(self):
s = self.name + "'s score is " + str(self.gamesWon)
return(s)
def winFunction(self):
self.gamesWon = self.gamesWon + 1
def count (self):
if self.counter < 4 :
return True 
else:
return False
def game(self):
randomnumber = random.randint(1,10)
print("Welcome to the Guessing Game! My number could be any number between 1-10!")
rawguessednumber = input("What is your guess?")
guessednumber = int(rawguessednumber)
if randomnumber == guessednumber :
print("You guessed it right!")
self.gamesWon = self.gamesWon + 1
self.counter = self.counter +1
elif randomnumber < guessednumber :
print("You guessed too high!")
elif randomnumber > guessednumber :
print("You guessed too low!")
def main():
player1 = Players("Ana")
player2 = Players("Ryan")
while player1.count() and player2.count():
gamesWon = player1.game()
gamesWon = player2.game()
print (player1)
print (player2)
print ("The End!")
main()

我稍微改变了一下结构,使其更易于遵循。首先,我添加了一个Game类,因为让它控制一组Player对象更有意义。我相信我已经达到了你想要的改变和游戏规则。我还添加了一些概念供您参考。这个程序将根据需要接纳尽可能多的玩家,而不是在每次迭代中对每个玩家进行硬编码。这是:

import random

class Player:
def __init__(self, name):
"""
:param name: Name of the player
"""
self.name = name
self.games_won = 0
self.random_number = None
def win_function(self):
self.games_won += 1
def count(self):
# In the original code, gamesWon = counter. Just use either one.
# Also, games_won needs to be < 3 for three rounds.
if self.games_won < 3:
return True
else:
print(f'{self.name} has won!')
return False
def get_rand_number(self):
self.random_number = random.randint(1, 10)
def guess(self):
guess = input(f"{self.name} Guess: ")
guess = int(guess)
if guess == self.random_number:
print('You guessed it right!')
self.get_rand_number()  # Get another random number for them to choose
self.games_won += 1
elif guess < self.random_number:
print('You guessed too low!')
else:
print('You guessed too high!')
def __str__(self):
return f"{self.name}'s Score: {self.games_won}"

class Game:
def __init__(self, *players):
"""
:param players: Some arbitrary number of Player objects
"""
self.players = players
# list comprehension (assigns random number to each player)
[player.get_rand_number() for player in self.players]
def start(self):
print('Welcome to the guessing game! Pick a number between 1 and 10. First to reach 3 guesses wins!n')
while all([player.count() for player in self.players]):
for player in self.players:
player.guess()
print(player)
print()  # Adds space between players to make it more readable

player1 = Player('Ana')
player2 = Player('Ryan')
game = Game(player1, player2)
game.start()

你应该研究的一件事是try-and-except块,以处理玩家输入"dog"而不是数字的问题。以下是输出示例:

Welcome to the guessing game! Pick a number between 1 and 10. First to reach 3 guesses wins!
Ana Guess: 5
You guessed too high!
Ana's Score: 0
Ryan Guess: 5
You guessed too high!
Ryan's Score: 0
Ana Guess: 4
You guessed too high!
Ana's Score: 0
Ryan Guess: 4
You guessed too high!
Ryan's Score: 0
Ana Guess: 3
You guessed too high!
Ana's Score: 0
Ryan Guess: 3
You guessed too high!
Ryan's Score: 0
Ana Guess: 2
You guessed too high!
Ana's Score: 0
Ryan Guess: 2
You guessed it right!
Ryan's Score: 1
Ana Guess: 1
You guessed it right!
Ana's Score: 1
Ryan Guess: 5
You guessed too low!
Ryan's Score: 1
Ana Guess: 5
You guessed too high!
Ana's Score: 1
Ryan Guess: 6
You guessed too low!
Ryan's Score: 1
Ana Guess: 4
You guessed too high!
Ana's Score: 1
Ryan Guess: 7
You guessed too low!
Ryan's Score: 1
Ana Guess: 3
You guessed too high!
Ana's Score: 1
Ryan Guess: 8
You guessed too low!
Ryan's Score: 1
Ana Guess: 2
You guessed it right!
Ana's Score: 2
Ryan Guess: 9
You guessed too low!
Ryan's Score: 1
Ana Guess: 5
You guessed too high!
Ana's Score: 2
Ryan Guess: 10
You guessed it right!
Ryan's Score: 2
Ana Guess: 1
You guessed too low!
Ana's Score: 2
Ryan Guess: 5
You guessed too low!
Ryan's Score: 2
Ana Guess: 2
You guessed too low!
Ana's Score: 2
Ryan Guess: 5
You guessed too low!
Ryan's Score: 2
Ana Guess: 3
You guessed it right!
Ana's Score: 3
Ryan Guess: 5
You guessed too low!
Ryan's Score: 2
Ana has won!

在main((中的while循环之后,生成一个if语句,如下所示:

if player1.count:
print("Player 1 wins!")
else:
print("Player 2 wins!")

这只是检查谁在知道有人结束了比赛后先拿到3分。

最新更新