新手來蟒蛇,並在我最新的程序中遇到困難。簡單地說,我試圖編寫一個用戶輸入變量的遞減循環,如果可能的話。基本上,我有一個全局常量設置爲例如如圖13所示,每當程序循環時,它提示用戶輸入一個值,然後該用戶值被剃掉13,直到它達到0.問題在於它確實刮掉了它,但是當它重申它將值重置爲13並且僅移除當前迭代值輸入。所以如果每次迭代輸入2次,它就會將其減少到11次......但我以2次爲例再次以11,8,5等等爲例或以3爲例10,7, 4名....任何幫助球員將非常感激,乾杯:)基本遞減循環 - PYTHON
a = 13
def main():
runLoop()
def runLoop():
while other_input_var > 0: # guys this is my main score accumulator
# variable and works fine just the one below
b=int(input('Please enter a number to remove from 13: '))
if b != 0:
shave(a, b)
def shave(a, b):
a -= b
print 'score is %d ' % a
if a == 0:
print "Win"
main()
查看'global'關鍵字,然後閱讀所有可能的關於爲什麼使用它是一個很好的跡象表明你正在做一些可怕的錯誤。 – geoffspear 2012-04-17 13:34:37
我明白了,所以變量應該在函數shave()中進行本地化?是的,這是我一直堅持的作業的一部分。 – user1291271 2012-04-17 14:03:03