我有兩個初學者程序,都使用'while'函數,一個正常工作,另一個讓我卡在一個循環中。第一個程序是這個;初學者蟒蛇 - 卡在循環中
num=54
bob = True
print('The guess a number Game!')
while bob == True:
guess = int(input('What is your guess? '))
if guess==num:
print('wow! You\'re awesome!')
print('but don\'t worry, you still suck')
bob = False
elif guess>num:
print('try a lower number')
else:
print('close, but too low')
print('game over')``
它給出了可預測的輸出;
The guess a number Game!
What is your guess? 12
close, but too low
What is your guess? 56
try a lower number
What is your guess? 54
wow! You're awesome!
but don't worry, you still suck
game over
但是,我也有這個程序,這是行不通的;
#define vars
a = int(input('Please insert a number: '))
b = int(input('Please insert a second number: '))
#try a function
def func_tim(a,b):
bob = True
while bob == True:
if a == b:
print('nice and equal')
bob = False
elif b > a:
print('b is picking on a!')
else:
print('a is picking on b!')
#call a function
func_tim(a,b)
其中輸出;
Please insert a number: 12
Please insert a second number: 14
b is picking on a!
b is picking on a!
b is picking on a!
...(repeat in a loop)....
有人可以讓我知道爲什麼這些程序是不同的?謝謝!
「while function」?你的意思是「聲明」嗎? – 2010-03-16 10:06:37
'== True'永遠不是正確的路要走。而不是'if a == True:',總是比較喜歡'如果a:'。 – 2010-03-16 16:15:34