python - 我的代码冻结并停止,它有什么问题?
问题描述
我是 python 新手,所以如果它真的很基本,请不要感到惊讶,但我一直在尝试编写这段代码来询问数学问题,然后保存分数以便在循环开始时再次显示它们,但它不保存分数。我应该改变什么?这是代码
scores = []
names = []
while True:
f = open("highscore.txt", "r")
for line in f:
line = line.strip("\n")
line = line.split(" ")
names.append(line[0])
scores.append(int(line[1]))
print(f.read())
for pos in range(len(names)) :
print(pos + 1, names[pos], scores[pos])
f.close()
score = 0
print("hello, welcome to maths game")
print("\nQuestion 1: what is 2 x 2 x 2?")
answer = int(input("your answer >"))
if answer == 8:
print("correct")
score = score + 1
print("your score is ", score)
else:
print("incorrect")
print("the score is ", score)
print("\nQuestion 2: what is 34 x 2?")
answer = int(input("your answer >"))
if answer == 68:
print("correct")
score = score + 1
print("your score is", score)
else:
print("incorrect")
print("the score is", score)
name = input("what is your name?")
position = 0
for compare_score in scores :
if score < compare_score:
position = position + 1
scores.insert(position, score)
names.insert(position, name)
scores = scores[:5]
names = names[:5]
f = open("highscore.txt", "w")
for pos in range (len(names)):
f.write(names[pos] + " " + scores[pos])
它没有给出任何类型的错误消息,只是循环返回并且不保存名称,也不保存分数
解决方案
Alain T. 的回答已经说明了您遇到的根本原因。循环永远不会停止,这在您看来是“冻结”,因为您(作为用户/开发人员)没有看到循环仍在运行的输出或指标。所以实际上这里没有什么冻结..它只是永远运行。
出于这个原因,我想添加一个简短的说明,如何在下一次自己钻取问题。这里的关键字很明显:调试。
调试意味着:“找出你的代码在执行时做了什么”
一种非常简单但(至少对于小程序而言)非常有效的方法是使用一个或多个print()
语句。这些可用于显示变量的值、对象的属性或只是一些像print("I am before the loop")
知道执行运行/停止的语句。
一种可能是:(查看打印语句)
while True:
print("in while") #<-- this one
...
print("before loop") #<-- this one
for compare_score in scores :
print("in loop") #<-- this one repeats....
if score < compare_score:
position = position + 1
scores.insert(position, score)
names.insert(position, name)
scores = scores[:5]
names = names[:5]
print("After loop") #<-- never see this one
f = open("highscore.txt", "w")
for pos in range (len(names)):
f.write(names[pos] + " " + scores[pos])
再次运行您的程序应该会打印出:
in while
before loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
...
等等......所以你现在知道的是:
- 循环之前的所有内容至少执行
- 循环永远运行。
所以现在是时候深入挖掘循环内部了。最有趣的是检查循环退出所依赖的变量。在您的情况下,这是scores
列表的长度:
for compare_score in scores:
所以循环一直运行,直到分数列表中没有更多分数可供比较。
因此,最好print()
检查列表的长度是否以及如何减少,直到没有更多分数可供比较。
所以添加如下内容:检查print()
包含的两个语句len(scores)
for compare_score in scores:
print("in loop")
if score < compare_score:
position = position + 1
scores.insert(position, score)
names.insert(position, name)
scores = scores[:5]
names = names[:5]
print(len(scores)) #<--- this one
# or a bit nicer as f-string:
print(f"len score: {len(scores)}") #<--- this one
print("After loop")
两者都显示scores
列表的长度。前者只是做得更好一点。调试还有很多。VSCode、Pycharm 等许多工具支持更复杂的方法来逐步执行代码、设置断点、检查对象和变量。但对于小型和简单的项目以及当重点是学习时,即时反馈和重复。至少在我看来。Print()
调试以非常简单的方式为您提供了很多洞察力。
哦,如果你读到这里:
"Welcome to the community" Just jokin', welcome !! ;)"
推荐阅读
- java - 如何用Java表示已知大小的未知大小元素集?
- python - model.save 收到 Not JSON Serializable 错误。张量流1.14.0
- angular - 单击任何字母时的角度提及触发器
- python-3.x - 如何在不使用 import re 的情况下检查密码(用户输入)的有效性?
- javascript - javascript/Typescript 在 .then 内部返回 array.filter 回调
- regex - 用忽略替换字符串中的确切数字。(点)在python中
- javascript - 在输入标签中输入数字时发布请求
- mongodb - Symfony Api Platform 400 Not Found Error 使用 MongoDB ODM 执行默认项目操作时
- php - 致命错误:调用未定义的方法 ShopProductWriter::getProducer()
- python - python文件写入显示UnicodeEncodeError的错误