Reputation:
I am writing an ATM code and I am having trouble with a bit of code, which despite my best efforts I cannot fix. Here is the error message:
line 35, in Withdraw
back2_menu = int(input("Do You Wish To Have Another Transation? "))
ValueError: invalid literal for int() with base 10: 'Y'
And here is the troublesome piece of code
def Withdraw2(self):
amount = int(input("How Much Do You Wish To Withdraw: \n £"))
if int(amount) <= self.balance:
self.balance = self.balance - amount
print("Withdrawl Accepted. \nYour New Balance Is: £" + str(self.balance))
else:
print("Withdrawl Denied. \n You Have £" + str(self.balance) + "Within Your Account")
again = int(input("Do You Wish To Enter Another Amount"))
if again in ("Y", "y", "Ye", "ye", "Yes", "yes"):
print(atm.Withdraw2())
else:
backmenu = str(input("Do You Wish To Have Another Transation? "))
if backmenu in ("Y", "y", "Ye", "ye", "Yes", "yes"):
print(atm.Menu())
else:
print(atm.End())
I am just wondering how to fix this so my program runs smoothly. Many Thanks
Upvotes: 1
Views: 2740
Reputation: 221
The int(...) function turns a string integer (i.e. "123") into an integer integer (i.e. 123) without quotes.
"y" does not conform to integer syntax, so int("y") is not valid.
Instead of:
again = int(input("Do You Wish To Enter Another Amount"))
Do something like a few lines further, and say:
again = str(input("Do You Wish To Enter Another Amount"))
Then you won't get that error, which is caused by trying to parse out an integer from 'Y'.
Note that the earlier input:
amount = int(input("How Much Do You Wish To Withdraw: \n £"))
Is valid, if you enter a number. Beware, though, that int(3.4) will truncate to 3.
Upvotes: 1