在文本文件中查找数字最低的行

时间:2013-04-11 04:25:02

标签: python python-2.7 file-io

我正在研究用Python编写的猜谜游戏,用户猜测某个范围内的数字(1-1000),并将高分(最低尝试次数)附加到文本文件中。

我需要找到一种方法来读取所有数字,并将最低值打印(读取)到屏幕上。是否有捷径可寻?到目前为止,我一直在遇到问题。这是我代码的一部分:

if guess == the_number:
        print "--------------------------------------"
        print "You guessed correctly! The number was:", the_number
        print "And it only took you", no_of_tries, "tries!"
        text_file = open("scores.txt", "a")
        the_scores = text_file.write(str(no_of_tries) + ' ')

1 个答案:

答案 0 :(得分:0)

如果您将所有分数用空格分隔,则可以通过此方法检索它们

input=open('scores.txt')
scores=input.read()
scores=scores.split()
print min(scores)

打印最小猜测

相关问题