我已经用Python编写了一个数字猜谜游戏的程序,该程序我几乎完成了。但是,有一个小问题我不知道。游戏结束后,我希望将每个玩家的统计信息打印到具有以下信息的.txt文件中:
用户名|输赢|猜数。
我不知道如何在下一行为每个玩家生成赢/赔统计:
f.write(str(playerName)+ '|' +str(numberOfGuesses)+'\n').
如果有人可以提供一些建议,将不胜感激。我已经显示了部分代码,如下所示:
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())
答案 0 :(得分:0)
非常简单。 为了澄清,如果won == True,您想在行上写“ Won”,如果won == False,您想在行上写“ Loss”吗?
win_or_loss = 'Loss'
if won:
win_or_loss = 'Win'
然后,您只需在写入文件时使用此变量
f.write(str(playerName)+ '|' + win_or_loss + '|' +str(numberOfGuesses)+'\n')
此外,您不需要将playerName包裹在str()中,因为它已经是一个字符串。
答案 1 :(得分:0)
由于您有[numCorrect, totalGuesses]
,因此我假设您正在尝试保持每个玩家的得分统计。在这种情况下,建议您创建一个以玩家姓名为关键词的字典。值可以是每个玩家numCorrect
的列表。您将在每个玩家回合时适当地更新totalGuesses
和<tbody style="
white-space: nowrap;
">
<?php
foreach($student_subject_list AS $SSL)
{
$subject_name = $SSL["name"];
if(in_array($subject_name, array_keys($result)))
{
$total_score = 0;
$avg = count($result[$subject_name]);
$test_results = "";
$i = 1;
foreach($result[$subject_name] AS $SN)
{
if($i == 1)
{
$ie = "1<sup>st</sup>";
}
elseif($i == 2)
{
$ie = "<br>2<sup>nd</sup>";
}
elseif($i == 3)
{
$ie = "<br>3<sup>rd</sup>";
}
else
{
$ie = "<br>4<sup>th</sup>";
}
$test_results .= "$ie Test: $SN ";
$total_score += $SN;
$avg_score = $total_score/$avg;
$i++;
}
}
else
{
$total_score = $test_results = "N/A";
}
?>
<tr>
<td style="border: 1px solid black; font-size:11px;width:120px;white-space: nowrap;height:30px;"><?=$subject_name?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?=$test_results?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?=$avg_score?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?=$remark?></td>
</tr>
<?php
}
?>
</tbody>
。最后,您将浏览该词典并处理结果。
答案 2 :(得分:0)
这是要实现的逻辑,因此您可以获得赢/赔状态。
对于每个游戏,您将拥有以下内容
玩家名称,胜利或失败标志,总猜测。
读取统计文件以查看播放器是否存在。如果播放器存在,则检索以下信息:
玩家姓名,获胜数,输球数,输赢统计,最低猜测数
* 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
对于所有后续条目,您需要根据新的计算来更新文件中的记录,如下所示。
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+
将此统计信息写回文件。这将跟踪玩家的状态,您可以使用它来保持每次玩家播放时的统计信息
由于使用上述逻辑将有大量I / O,因此您可能希望将文件读入内存并在内存中进行计算,然后作为正常程序退出的一部分写入文件。一个挑战是,如果程序突然崩溃,数据将丢失。
希望这可以帮助您实现良好的逻辑。
我想在注释中写这个,但是超过了最大字符数。因此,将其放在答案注释中。我希望您编写代码,因为它将帮助您实现逻辑。