简体   繁体   English

Python 猜数游戏中的程序赢/输统计

[英]Program Win/Loss statistics in Number Guessing Game for Python

I've written a program for a Number guessing game in Python which i have almost completed.我已经为 Python 中的数字猜谜游戏编写了一个程序,我几乎已经完成了。 However, there is one small problem i can't figure out.但是,有一个小问题我无法弄清楚。 After the game finishes I want the statistics to be printed out for each player to a.txt with the following information:游戏结束后,我希望将每个玩家的统计数据打印到 a.txt 中,其中包含以下信息:

Username |用户名 | Win or Loss |输赢 | Number of Guesses.猜测数。

I can't figure out how to generate the Win/Loss statistic in the following line for each player:我无法弄清楚如何在以下行中为每个玩家生成赢/输统计信息:

f.write(str(playerName)+ '|' +str(numberOfGuesses)+'\n').

If anyone could please provide some advice it would be greatly appreciated.如果有人可以请提供一些建议,将不胜感激。 I have displayed part of the code as follows:我已显示部分代码如下:

won = False

while numberOfGuesses < maxGuesses:
   guess = int(input('What is your guess?: '))
   numberOfGuesses += 1
   if guess < number:
        print('Too low')
   if guess > number:
        print('Too high')
   if guess == number:
        won = True
        break
   print('You have', str(maxGuesses - numberOfGuesses) + ' guesses remaining\n')

if won:
    print('Congratulations!!! You guessed the number in ' + str(numberOfGuesses) + ' tries!')
else:
    print('Game Over!!! You did not guess the correct number, the number was ' + str(number))


f = open('Statistics.txt', 'a')
f.write(str(playerName)+ '|' +str(numberOfGuesses)+'\n')
f.close()
f = open('Statistics.txt', 'r')
print(f.read())

It's pretty, simple.很漂亮,很简单。 To clarify, if won == True, you want to write "Won" to the line and if won == False, you want to write "Loss" to the line right?澄清一下,如果 won == True,你想在该行写“Won”,如果 won == False,你想在该行写“Loss”对吗?

win_or_loss = 'Loss'
if won:
    win_or_loss = 'Win'

Then you just use this variable when you write to the file然后,您只需在写入文件时使用此变量

f.write(str(playerName)+ '|' + win_or_loss + '|' +str(numberOfGuesses)+'\n')

Also, you don't need to wrap playerName in str() since it is already a string.此外,您不需要将 playerName 包装在 str() 中,因为它已经是一个字符串。

Since you have playerName in the mix, I assume you're trying to keep score stats per player.由于您有playerName混合,我假设您正在尝试保持每个玩家的得分统计。 In that case, I suggest you create a dictionary keyed on player names.在这种情况下,我建议您创建一个以玩家姓名为关键字的字典。 The values can be lists of [numCorrect, totalGuesses] per player.这些值可以是每个玩家的[numCorrect, totalGuesses]列表。 You would update numCorrect and totalGuesses during each player's turn as appropriate.您将在每个玩家的回合中酌情更新numCorrecttotalGuesses In the end, you would walk through that dictionary and process the results.最后,您将遍历该字典并处理结果。

Here's the logic to implement so you can get the Win/Loss stats.这是实现的逻辑,因此您可以获得赢/输统计信息。

For each game, you will have the following对于每场比赛,您将拥有以下内容

Player Name, Win or Loss flag, Total Guesses.玩家姓名、输赢标志、总猜测数。

Read statistics file to see if player exists.读取统计文件以查看播放器是否存在。 If player exists, then retrieve the following information:如果播放器存在,则检索以下信息:

Player Name, Win Count, Loss Count, Win-Loss Stats, Lowest Number of Guesses玩家姓名、获胜次数、失败次数、胜负统计、最低猜测次数

* Win Count will be # of times player has won in the past

* Loss Count will be # of times player has lost in the past

* Win-Loss stat will be Win / (Win + Loss). If you want % then x by 100

* Lowest Number of Guesses for the first entry will be numberOfGuesses

For all subsequent entries, you need to update the record in the file based on new calculations as shown below.对于所有后续条目,您需要根据新的计算更新文件中的记录,如下所示。

If current game is win, win_count += 1
If current game is loss, loss_count += 1
win_loss stat will remain win_count / (win_count + loss_count)

if numberOfGuesses < lowest_number_of_guesses: lowest_number_of_guesses = numberOfGuesses. 

#you can modify this with walrus option if you use 3.8+

Write back this stats to the file.将此统计信息写回到文件中。 This will keep track of the status for the player and you can use this to keep the stats updated every time the player plays这将跟踪播放器的状态,您可以使用它来保持每次播放器播放时更新的统计信息

Since you are going to have a lot of I/O with the above logic, you may want to read the file into memory and calculate this in memory, then write to file as part of normal program exit.由于上述逻辑将有大量 I/O,您可能希望将文件读入 memory 并在 memory 中计算,然后作为正常程序退出的一部分写入文件。 One challenge is that the data will be lost if the program abruptly crashes.一个挑战是,如果程序突然崩溃,数据将会丢失。

Hope this helps you implement a good logic.希望这可以帮助您实现一个好的逻辑。

I wanted to write this in comments but the max chars exceeded.我想在评论中写这个,但是超过了最大字符数。 So put this in the answer comments.所以把它放在答案评论中。 I would prefer you write the code as it will help you with the implementation of the logic.我希望您编写代码,因为它将帮助您实现逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM