多次更改循环中变量的值
Change value of variable in a loop, multiple times
我有一个银行程序,您可以在其中执行多项操作,并且 var 余额需要根据用户输入进行更新。它第一次这样做,并在下一个 运行 中使用新值。但是,如果您 运行 第三次循环,它会在第一次 运行 中使用平衡输出,而不是第二次。如果你 运行 它四次,它仍然使用第一个值,依此类推。
我对编程还是很陌生,但我的理论是第二个循环无法 return 平衡的新值,所以它只会卡在第一个循环上。但我不知道如何解决这个问题。
这里有人有什么想法吗?谢谢。
balance = 500
def main(balance):
menu = int(input('Press 1 for balance, 2 for deposit, 3 for withdrawal or 4 for interest return: '))
print()
if menu == 1:
print(balance)
elif menu == 2:
dep = int(input('How much do you want to deposit? '))
print()
balance = (balance + dep)
print('Your new balance is', balance)
print()
if balance <= 999999:
interest = 0.01
print('Your interest is standard, 0.01%')
if balance >= 1000000:
interest = 0.02
print('Your intrest is premium, 0.02%!')
elif menu == 3:
wit = int(input('How much do you want to withdraw? '))
print()
balance = (balance - wit)
print('Your new balance is', balance)
print()
if balance <= 999999:
interest = 0.01
print('Your intrest is standard, 0.01%')
if balance >= 1000000:
interest = 0.02
print('Your interest is premium, 0.02%!')
elif menu == 4:
if balance <= 999999:
interest = 0.01
if balance >= 1000000:
interest = 0.02
interest_return = (balance * interest)
balance = (balance + interest_return)
print('Your interest is', interest, 'that makes your intrest return', interest_return, 'and your new balance', balance)
return balance
balance = main(balance)
while True:
print()
restart = str(input('Would you like to do more? Press y for yes or n for no: '))
if restart == 'n':
print('Thank you for using the automatic bank service!')
break
elif restart == 'y':
main(balance)
else:
print()
print('Invalid input, press y for yes or n for no')
continue
您需要使用用户输入更新 while 循环中的 balance
变量。将行 main(balance)
更新为 balance = main(balance)
tldr;
balance = 500
while True:
print()
restart = str(input('Would you like to do more? Press y for yes or n for no: '))
if restart == 'n':
print('Thank you for using the automatic bank service!')
break
elif restart == 'y':
balance = main(balance) ########### HERE
else:
print()
print('Invalid input, press y for yes or n for no')
continue
解释:在您的原始代码中,您在每个循环中都分配了新值,因此
balance(main)
总是从分配的值开始,并且从不更改它,因为在 main 函数的范围内更改被添加到 local 平衡,而在函数之外你有 全球平衡。功能正在对本地进行更改,返回并打印本地,而全局始终相同。
为了更好地理解这一点,尽量不要将局部变量和全局变量命名为相似的,例如:
def main(local_balance):
menu = int(input('Press 1 for local_balance, 2 for deposit, 3 for withdrawal or 4 for interest return: '))
print()
if menu == 1:
print(local_balance)
elif menu == 2:
dep = int(input('How much do you want to deposit? '))
print()
local_balance = (local_balance + dep)
print('Your new local_balance is', local_balance)
print()
if local_balance <= 999999:
interest = 0.01
print('Your interest is standard, 0.01%')
if local_balance >= 1000000:
interest = 0.02
print('Your intrest is premium, 0.02%!')
elif menu == 3:
wit = int(input('How much do you want to withdraw? '))
print()
local_balance = (local_balance - wit)
print('Your new local_balance is', local_balance)
print()
if local_balance <= 999999:
interest = 0.01
print('Your intrest is standard, 0.01%')
if local_balance >= 1000000:
interest = 0.02
print('Your interest is premium, 0.02%!')
elif menu == 4:
if local_balance <= 999999:
interest = 0.01
if local_balance >= 1000000:
interest = 0.02
interest_return = (local_balance * interest)
local_balance = (local_balance + interest_return)
print('Your interest is', interest, 'that makes your intrest return', interest_return, 'and your new local_balance', local_balance)
return local_balance
global_balance = 500
while True:
print()
restart = str(input('Would you like to do more? Press y for yes or n for no: '))
if restart == 'n':
print('Thank you for using the automatic bank service!')
break
elif restart == 'y':
global_balance = main(global_balance)
else:
print()
print('Invalid input, press y for yes or n for no')
continue
我有一个银行程序,您可以在其中执行多项操作,并且 var 余额需要根据用户输入进行更新。它第一次这样做,并在下一个 运行 中使用新值。但是,如果您 运行 第三次循环,它会在第一次 运行 中使用平衡输出,而不是第二次。如果你 运行 它四次,它仍然使用第一个值,依此类推。
我对编程还是很陌生,但我的理论是第二个循环无法 return 平衡的新值,所以它只会卡在第一个循环上。但我不知道如何解决这个问题。
这里有人有什么想法吗?谢谢。
balance = 500
def main(balance):
menu = int(input('Press 1 for balance, 2 for deposit, 3 for withdrawal or 4 for interest return: '))
print()
if menu == 1:
print(balance)
elif menu == 2:
dep = int(input('How much do you want to deposit? '))
print()
balance = (balance + dep)
print('Your new balance is', balance)
print()
if balance <= 999999:
interest = 0.01
print('Your interest is standard, 0.01%')
if balance >= 1000000:
interest = 0.02
print('Your intrest is premium, 0.02%!')
elif menu == 3:
wit = int(input('How much do you want to withdraw? '))
print()
balance = (balance - wit)
print('Your new balance is', balance)
print()
if balance <= 999999:
interest = 0.01
print('Your intrest is standard, 0.01%')
if balance >= 1000000:
interest = 0.02
print('Your interest is premium, 0.02%!')
elif menu == 4:
if balance <= 999999:
interest = 0.01
if balance >= 1000000:
interest = 0.02
interest_return = (balance * interest)
balance = (balance + interest_return)
print('Your interest is', interest, 'that makes your intrest return', interest_return, 'and your new balance', balance)
return balance
balance = main(balance)
while True:
print()
restart = str(input('Would you like to do more? Press y for yes or n for no: '))
if restart == 'n':
print('Thank you for using the automatic bank service!')
break
elif restart == 'y':
main(balance)
else:
print()
print('Invalid input, press y for yes or n for no')
continue
您需要使用用户输入更新 while 循环中的 balance
变量。将行 main(balance)
更新为 balance = main(balance)
tldr;
balance = 500
while True:
print()
restart = str(input('Would you like to do more? Press y for yes or n for no: '))
if restart == 'n':
print('Thank you for using the automatic bank service!')
break
elif restart == 'y':
balance = main(balance) ########### HERE
else:
print()
print('Invalid input, press y for yes or n for no')
continue
解释:在您的原始代码中,您在每个循环中都分配了新值,因此
balance(main)
总是从分配的值开始,并且从不更改它,因为在 main 函数的范围内更改被添加到 local 平衡,而在函数之外你有 全球平衡。功能正在对本地进行更改,返回并打印本地,而全局始终相同。 为了更好地理解这一点,尽量不要将局部变量和全局变量命名为相似的,例如:
def main(local_balance):
menu = int(input('Press 1 for local_balance, 2 for deposit, 3 for withdrawal or 4 for interest return: '))
print()
if menu == 1:
print(local_balance)
elif menu == 2:
dep = int(input('How much do you want to deposit? '))
print()
local_balance = (local_balance + dep)
print('Your new local_balance is', local_balance)
print()
if local_balance <= 999999:
interest = 0.01
print('Your interest is standard, 0.01%')
if local_balance >= 1000000:
interest = 0.02
print('Your intrest is premium, 0.02%!')
elif menu == 3:
wit = int(input('How much do you want to withdraw? '))
print()
local_balance = (local_balance - wit)
print('Your new local_balance is', local_balance)
print()
if local_balance <= 999999:
interest = 0.01
print('Your intrest is standard, 0.01%')
if local_balance >= 1000000:
interest = 0.02
print('Your interest is premium, 0.02%!')
elif menu == 4:
if local_balance <= 999999:
interest = 0.01
if local_balance >= 1000000:
interest = 0.02
interest_return = (local_balance * interest)
local_balance = (local_balance + interest_return)
print('Your interest is', interest, 'that makes your intrest return', interest_return, 'and your new local_balance', local_balance)
return local_balance
global_balance = 500
while True:
print()
restart = str(input('Would you like to do more? Press y for yes or n for no: '))
if restart == 'n':
print('Thank you for using the automatic bank service!')
break
elif restart == 'y':
global_balance = main(global_balance)
else:
print()
print('Invalid input, press y for yes or n for no')
continue