如果有人放入浮子,则会出现INT(输入)错误的问题。如何修复
我目前正在为我的编码课做一个项目。提示是制作ATM接口。
我的所有代码当前都在工作,但是当涉及押金的功能()时,它要求输入一个我使用int(输入)的整个数字,说某人输入了像45346.4这样的浮标,它会出现错误。有解决问题吗?
这是我目前用于存款功能的代码。余额已经在此功能之外给出。
def deposit():
balanced = balance
print(f'Your current balance is ${balanced}\n------------------------------')
print('How much money would you like to deposit?\n---------------------------\n You can only deposit in whole numbers')
deposit_amount = int(input('Enter here:'))
if deposit_amount.is_integer():
balance_a_d = balanced + deposit_amount
print(f'You Current Balance is ${balance_a_d}\n-------------------------------\nHave a great day!')
quit()
else:
print('----------------------------\nThat is not a whole number please try again\n----------------------------')
deposit()
Im currently working on a project for my coding class. The prompt is to make an atm interface.
All of my code is currently working but when it comes to the function of deposit() it asks for a whole number to be entered where I use int(input) say someone inputs a float like 45346.4 it comes up with an error. Is there a fix to this?
here is my code currently for the deposit function. The balance is already given outside of this function.
def deposit():
balanced = balance
print(f'Your current balance is ${balanced}\n------------------------------')
print('How much money would you like to deposit?\n---------------------------\n You can only deposit in whole numbers')
deposit_amount = int(input('Enter here:'))
if deposit_amount.is_integer():
balance_a_d = balanced + deposit_amount
print(f'You Current Balance is ${balance_a_d}\n-------------------------------\nHave a great day!')
quit()
else:
print('----------------------------\nThat is not a whole number please try again\n----------------------------')
deposit()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
时
循环和尝试
block继续要求输入,直到不再提高value> value> value>。
这样的事情:
You could use a
while
loop and atry
block to keep asking for input untilValueError
is no longer raised.Something like this:
我有一个通用输入功能,该功能易于使用,并节省了许多重复的代码。
如果我只想输入文本输入,那么:
当然,对于纯文本,这并不是必需的。
如果我想要一个浮点,则:
或int:
这可以节省您不必“包装” try/除外的所有输入
I have a general purpose input function which is simple to use and saves a lot of repeated code.
If I just want text input then:
Of course, for plain text, this isn't really necessary.
If I want a float then:
Or int:
This saves you from having to "wrap" all your inputs in try/except as it's all handled for you