如果有人放入浮子,则会出现INT(输入)错误的问题。如何修复

发布于 2025-02-08 12:32:35 字数 865 浏览 0 评论 0原文

我目前正在为我的编码课做一个项目。提示是制作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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2025-02-15 12:32:35

您可以使用循环和尝试 block继续要求输入,直到不再提高value> value> value>。

这样的事情:

number = None
while number == None:
    try:
        number = int(input('Enter here:'))
    except ValueError as e:
        print('Please enter a whole number.')

You could use a while loop and a try block to keep asking for input until ValueError is no longer raised.

Something like this:

number = None
while number == None:
    try:
        number = int(input('Enter here:'))
    except ValueError as e:
        print('Please enter a whole number.')
相思故 2025-02-15 12:32:35

我有一个通用输入功能,该功能易于使用,并节省了许多重复的代码。

def getInput(prompt, t=str):
    while True:
        v = input(f'{prompt}: ')
        try:
            return t(v)
        except ValueError:
            print('Invalid input')

如果我只想输入文本输入,那么:

getInput('Enter some text')

当然,对于纯文本,这并不是必需的。

如果我想要一个浮点,则:

getInput('Enter a float', float)

或int:

getInput('Enter an integer', int)

这可以节省您不必“包装” try/除外的所有输入

I have a general purpose input function which is simple to use and saves a lot of repeated code.

def getInput(prompt, t=str):
    while True:
        v = input(f'{prompt}: ')
        try:
            return t(v)
        except ValueError:
            print('Invalid input')

If I just want text input then:

getInput('Enter some text')

Of course, for plain text, this isn't really necessary.

If I want a float then:

getInput('Enter a float', float)

Or int:

getInput('Enter an integer', int)

This saves you from having to "wrap" all your inputs in try/except as it's all handled for you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文