我有一些代碼類似於此在while循環從定義函數訪問變量
v = '0'
def program():
x = input('1 or 2 ')
if x == '1':
print('it is 1')
v = '1'
elif x == '2':
print('it is 2')
v = '2'
while True:
program()
print(v)
然而,當我運行此代碼變量「V」總是打印出默認的0 爲什麼是不是它給了我在函數中賦值的變量?
哪個版本您使用的使用
raw_input
在Python? – pzp你會在這裏找到一個很好的答案:http:// stackoverflow。com/questions/929777/why-do-assigning-to-my-global-variables-not-work-in-python –