为什么“while”语句不能识别变量中的更改?

2024-05-05 15:11:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用while语句保持输入直到满足条件,但是它看不到变量值的变化并保持循环。它也开始跳过行,直到它自己终止。这看起来像递归,但我不太懂。这是我的代码…#加载

first = input("First lot #:   ")
last = input("Last lot #:  ")

for a in range(first,last +1):
    draw=raw_input("?:  ")

    while draw==(""):      #This is for erroneus input 
        print draw    #" while" wasn't seeing the change of value in draw so I added 
                      # print draw
        print "Error" #" print draw" is skipped after the 1st time thruogh the loop
        draw=raw_input("?:  ")
        print draw    # this line is skipped after 2nd time thruogh loop
##    
##    while draw>35:   #The same
##        print"Error"
##        draw=raw_input("?:  ")
##
##    num=(str(a),draw)
##    num=str(num)         # python thinks num is a tuple..another   
##    print num    
## 
##    f = open("pb_loader","a")
##    f.write(num)
##    f.close()
##    
##
##c=open("pb_loader","r")
##d=c.read()
##print d
##c.close()
##    

这是。。。。。。。。。你知道吗

First lot #:   1
Last lot #:  3
?:                    #entered "return"  
                      #printed empty space
Error
?:  2
2                     
?:  3                 #skipped "print draw" at top of loop
?:  3                 #skipped "print draw" at bottom of loop 
>>>                   #terminated itself

这似乎是非常基本的循环。如果有人有一些见解的话,我会非常感激的


Tags: oftheloopinputrawiserrornum
1条回答
网友
1楼 · 发布于 2024-05-05 15:11:48

轻微压痕问题

first = input("First lot #:   ")
last = input("Last lot #:  ")

for a in range(first,last +1):
    draw=raw_input("?:  ")
    while draw==(""):      #This is for erroneus input 
        print "Error" #" print draw" is skipped after the 1st time thruogh the loop
        draw=raw_input("?:  ")
    print draw    # this line is skipped after 2nd time thruogh loop

结果是

?:
Error
?:  1
1
?:  2
2
?:  3
3

相关问题 更多 >