python - 在我的 If 语句中,变量不会重置为 0
问题描述
while not(bool(won)):
#print(wordspec)
# print("_" * wordlen)
print(displayword)
answer = input("Any letters?")
if answer in vl.values():
print("Yes")
for x_l in wordspec:
print(str(spot_l)+"cha")
if spot_l < wordlen:
if vl[spot_l] == answer:
print(vl[spot_l])
ch[spot_l] = vl[spot_l]
displayword[spot_l] = ch[spot_l]
spot_l += 1
elif wordlen < spot_l:
spot_l = 0
所以在 1 个循环之后,spot_l 变量被卡在它可以达到的最大值。
输出是..
我第一次这样做:
0cha
1cha
a
2cha
3cha
a
4cha
5cha
a
6cha
第二,第三,第四等。我这样做的时间:
6cha
6cha
6cha
6cha
6cha
6cha
6cha
(我的话是 7 个字符,因此 for 循环 7 次)
解决方案
你输入spot_l == 0
,它检查是否相等0
(并且你忽略bool
它产生的,这使得这更明显是一个错误);如果你想0
通过赋值将它重置,你需要spot_l = 0
.
推荐阅读
- java - 嵌套循环的时间和空间复杂度
- web - 您如何将 jekyll 站点内容和代码分离到单独的分支中?
- javascript - 无法对齐网格项目之间的分隔线
- python - 无法将数据框的 html 内容转换为文本
- excel - 从 Excel 中的两个单元格获取 Outlook 约会的开始日期和开始时间
- postgresql - 使用 docker 的 Hasura 安装问题 - PostgreSQL 没有响应
- javascript - 如何使用 JavaScript 显示“倒计时”?
- javascript - 如何将代码片段存储在 mongo db 中?
- javascript - Firestore 事务顺序创建集合和更新文档
- node.js - 如何为生产环境设置 webpacker on rails?