money + 2
ノーオペレーションです。実際にmoney
新しい値を割り当てる必要があります
money = money + 2
# or
money += 2
しかし、その後、エラーが発生することがわかります-関数スコープ外の変数に代入することはできません. global
次のキーワードを使用できます。
global money
money += 2
これにより、関数内での値を変更できますmoney
。
ただし、推奨される方法はmoney
パラメーターとして渡すことです。
def gainM(money):
money += 2
Stats()
return money
if money == 1:
money = gainM(money)
2 番目のオプションを使用している場合 (これを使用する必要があります)、Stats
関数も変更してmoney
パラメーターを持たせる必要があります。
def Stats(money):
print
print "money " + str(money)
それ以外の場合、関数は の1
代わりに出力され3
ます。
別の推奨事項 - 文字列フォーマットを使用してください。
'money %d' % money # the old way
'money {}'.format(money) # the new and recommended way
money
次に、関数に渡しStats
ます。
def gainM(money):
money += 2
Stats(money)
return money