Python ATM余额不从if打印.else语句



我正试图回到python编码,因为我有一个基本的证书在它。我想我可以先做一个简单的自动取款机。然而,一旦我设置了我的代码,并尝试运行它,它不会打印出平衡,即使平衡变量被分配一个值。有人可以指导我在我做错了我的代码。

balance = 2000
withdraw = 0
deposit = 0

print("Welcome to the Heart Cold ATM")
print("Select 1 to View Currrent Balance")
print("Select 2 for Depsoit")
print("Select 3 for Withdraw")
print("Select 4 to Quit")
userInput = input("Please put the option that you want execute: ")
if userInput == 1:
print("Your current balance is " + balance)
elif userInput == 2:
depsoit = input("Enter the amount you would like to deposit: ")
balance += deposit
print(balance)
elif userInput == 3:
withdraw = input("How much would you like to withdraw: ")
balance -= withdraw
if withdraw > balance: 
print("Insufficient fund unavilable to withdraw from your account. Please re-enter a different amount")
print(balance)
else: 
ExitNow
balance = 2000
withdraw = 0
deposit = 0

print("Welcome to the Heart Cold ATM")
print("Select 1 to View Currrent Balance")
print("Select 2 for Depsoit")
print("Select 3 for Withdraw")
print("Select 4 to Quit")
userInput = input("Please put the option that you want execute: ")
if userInput == "1":
print("Your current balance is ", balance)
elif userInput == "2":
deposit = int(input("Enter the amount you would like to deposit: "))
balance += deposit
print(balance)
elif userInput == "3":
withdraw = int(input("How much would you like to withdraw: "))
balance -= withdraw
if withdraw > balance: 
print("Insufficient fund unavilable to withdraw from your account. Please re-enter a different amount")
print(balance)
else: 
exit()

你的错误是print(" something ")+ 16)你应该打印("东西"16)输入的输出是一个字符串你应该这样做

int(input("somthing"))

嘿,第一次在这里回答问题,但答案不是太疯狂。你不能将字符串与整数连接,所以你需要将变量'balance'强制转换为字符串,然后你可以连接并打印出整个字符串。

不是非常必要,但是我还添加了一个while循环,它将帮助您的ATM事务保持运行,直到您希望它以输入数字4结束。如果你输入的不是'1' '2'或'3',那么你之前设置的方式将会终止程序,而这种方式只会在'4'结束。

另一个问题是,当您尝试存取款时,您还需要将从用户接收到的输入转换为int,这样您就可以实际计算新的余额总额。

balance = 2000
withdraw = 0
deposit = 0
run = True
print("Welcome to the Heart Cold ATM")
print("Select 1 to View Currrent Balance")
print("Select 2 for Depsoit")
print("Select 3 for Withdraw")
print("Select 4 to Quit")
while run == True:
userInput = input("Please put the option that you want execute: ")
if userInput == '1':
print("Your current balance is:" + str(balance))
elif userInput == '2':
deposit = int(input("Enter the amount you would like to deposit: "))
balance = balance + deposit
print(balance)
elif userInput == '3':
withdraw = int(input("How much would you like to withdraw: "))
balance = balance - withdraw
if withdraw > balance:
print("Insufficient fund unavailable to withdraw from your account. Please re-enter a different amount")
print(balance)
elif userInput == '4':
print("loop is now ending")
run = False
else:
print("You have entered an invalid response.")

最新更新