Python 值返回为0
每当我运行该程序时,它都会将现金值返回到0。Python 值返回为0,python,Python,每当我运行该程序时,它都会将现金值返回到0。newcash+cash是一个从未分配给任何对象的结果。尝试调用cash+=newcash,或者cash=newcash+cashnewcash+cash是一个从未分配给任何内容的结果。尝试调用cash+=newcash,或者cash=newcash+cash而不是使用newcash+cash,您希望将变量newcash的值添加到变量cash中,如下所示 bank = 0 cash = 0 userinput = input(
newcash+cash
是一个从未分配给任何对象的结果。尝试调用cash+=newcash
,或者cash=newcash+cash
newcash+cash
是一个从未分配给任何内容的结果。尝试调用cash+=newcash
,或者cash=newcash+cash
而不是使用newcash+cash
,您希望将变量newcash
的值添加到变量cash
中,如下所示
bank = 0
cash = 0
userinput = input('would you like to work? ')
if userinput == 'y':
print('working...')
newcash = 100
newcash + cash
print(cash)
或者如果你喜欢一点“清洁剂”
不要使用
newcash+cash
,而是将变量newcash
的值添加到变量cash
中,如下所示
bank = 0
cash = 0
userinput = input('would you like to work? ')
if userinput == 'y':
print('working...')
newcash = 100
newcash + cash
print(cash)
或者如果你喜欢一点“清洁剂”
在您的代码中,执行加法时变量cash没有更新(
newcash+cash
),返回0是当前代码的正确答案。我想您需要这样的结果:
cash += newcash
现在您将得到答案。在您的代码中,变量cash在您进行加法时没有更新(
newcash+cash
),返回0是您当前代码的正确答案。我想您需要这样的结果:
cash += newcash
现在您将得到您的答案。这是因为您必须这样分配它:
bank = 0
cash = 0
userinput = input('would you like to work? ')
if userinput == 'y':
print('working...')
newcash = 100
cash += newcash
print(cash)
而不是
cash = newcash + cash
这是因为您必须这样分配它:
bank = 0
cash = 0
userinput = input('would you like to work? ')
if userinput == 'y':
print('working...')
newcash = 100
cash += newcash
print(cash)
而不是
cash = newcash + cash
也许您需要执行
cash=newcash+cash
来代替newcash+cash
?newcash+cash
返回一个从未分配给任何变量的值。要更改cash
的值,您需要将该值分配给该变量,即cash=newcash+cash
您可能需要执行cash=newcash+cash
来代替newcash+cash
?newcash+cash
返回一个从未分配给任何变量的值。要更改cash
的值,需要将该值分配给该变量,即cash=newcash+cash