简体   繁体   English

AttributeError: 'int' 对象没有属性 'isdigit' 和 NameError

[英]AttributeError: 'int' object has no attribute 'isdigit' and NameError

I am trying to make a program in which if I enter a positive number then it executes further but if I enter a negative number or a letter it should print 'House price must be a positive number' and it should ask for the input again.我正在尝试制作一个程序,如果我输入一个正数,它会进一步执行,但是如果我输入一个负数或一个字母,它应该打印“房价必须是一个正数”,它应该再次要求输入。 This is what I've done so far but when I enter an number I get an AttributeError and when I enter a letter I get a NameError .这是我到目前为止所做的,但是当我输入一个数字时,我得到一个AttributeError ,当我输入一个字母时,我得到一个NameError

import math

while True:
    home_price = input('Enter the price of your dreams house')
    if home_price.isdigit():
        if home_price > 0:    
            home_price = int(house_price)
            break
        else:
            print('House price must be a positive number only')

If you are using Python 2, whose input function does an eval(..) on the user input, and eval("2") returns an int , not a str .如果您使用的是 Python 2,其input函数对用户输入执行eval(..) ,并且eval("2")返回int ,而不是str

python2 -c 'print(eval("2").__class__)'
<type 'int'>

Your code will work on Python 2 if you replace input with raw_input , which doesnt do an eval .如果您将input替换为raw_input ,则您的代码将在 Python 2 上运行,而raw_input不执行eval

If you want to keep your loop running until you receive a positive integer, you can just test for negative integers / non digit values entered and then continue to the next iteration.如果您想保持循环运行直到收到正整数,您可以只测试输入的负整数/非数字值,然后继续下一次迭代。 Otherwise you can just break from your loop否则你可以从你的循环中解脱出来

while True:
    home_price = input('Enter the price of your dream house: ')

    if not home_price.isdigit() or not int(home_price) > 0:
        print('House price must be a positive integer only')
        continue

    print('The price of your dream house is: %s' % home_price)
    break

Enter the price of your dream house: a
House price must be a positive integer only
Enter the price of your dream house: -1
House price must be a positive integer only
Enter the price of your dream house: 1000
The price of your dream house is: 1000

I recommend exception handling.我推荐异常处理。 "-10".isdigit() returns false. "-10".isdigit() 返回 false。

import math

while True:
    home_price = input('Enter the price of your dreams house')
    try:
        if int(home_price) > 0:    
            house_price = int(home_price)
            break
        else:
            print('House price must be a positive number only')
    except ValueError:
        continue

Here is a different method:这是一种不同的方法:

home_price_gotten = False

while not home_price_gotten:
    try:
        home_price = input('Enter the price of your dream house\n')
        home_price = int(home_price)
        if home_price < 1:
            raise TypeError
        home_price_gotten = True
        break
    except ValueError:
        print("Home price must be a number, but '{}' is not.".format(home_price))
    except TypeError:
        print('Home price must be a positive number')


print("Success! Home price is : '{}'".format(home_price))

Basically, until the user has given a valid home price, the loop will ask him for one, and try to convert his input to a number.基本上,在用户给出有效的房屋价格之前,循环会要求他提供一个,并尝试将他的输入转换为一个数字。 If it fails, it's probably because the input is not a number.如果失败,可能是因为输入的不是数字。 If it's smaller than 1, it raises another error.如果它小于 1,则会引发另一个错误。 So you're covered.所以你被覆盖了。

BTW, this works in python 3 only.顺便说一句,这仅适用于 python 3。 For python 2, swap input() for raw_input()对于 python 2,将input()交换为raw_input()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:&#39;int&#39;对象没有属性&#39;isdigit&#39; - AttributeError: 'int' object has no attribute 'isdigit' 为什么我的代码不起作用 AttributeError: 'int' object has no attribute 'isdigit' - why my code not working AttributeError: 'int' object has no attribute 'isdigit' AttributeError:&#39;int&#39;对象没有用户输入的属性&#39;isdigit&#39; - AttributeError: 'int' object has no attribute 'isdigit' from user input AttributeError&#39;int&#39;对象在Ubuntu中的ipython中没有属性isdigit - AttributeError 'int' object has no attribute isdigit in ipython in ubuntu AttributeError: 'generator' object 没有属性 'isdigit' - AttributeError: 'generator' object has no attribute 'isdigit' Python-AttributeError:“ str”对象没有属性“ isDigit” - Python - AttributeError: 'str' object has no attribute 'isDigit' Python AttributeError:“系列”对象没有属性“ isdigit” - Python AttributeError: 'Series' object has no attribute 'isdigit' AttributeError:“列表”对象没有属性“ isdigit” - AttributeError: 'list' object has no attribute 'isdigit' 持续收到错误-builtins.AttributeError:“ int”对象没有属性“ isdigit” - Keep receiving error- builtins.AttributeError: 'int' object has no attribute 'isdigit' AttributeError:int对象没有属性 - AttributeError : int object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM