首页 > 解决方案 > 在我的 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 次)

标签: python

解决方案


你输入spot_l == 0,它检查是否相等0(并且你忽略bool它产生的,这使得这更明显是一个错误);如果你想0通过赋值将它重置,你需要spot_l = 0.


推荐阅读