为什么我收到有关'不支持的/'操作数类型的错误?

时间:2015-02-16 18:05:44

标签: python python-2.7

该程序应检查输入的数字是否为素数。

def checkPrime():
    primeASK = raw_input('''Please enter a prime number.''')
    print 'Your Number is %s.' %(primeASK)

    if primeASK.isdigit():
        primeASK = int(primeASK)
        number = 1

        while primeASK > number:
            print primeASK / number

            number += 1 


checkPrime()

这是我一直收到的错误(显然程序没有完成)

Traceback (most recent call last):
  File "C:/Python27/Lib/idlelib/primeChecker.py", line 14, in <module>
    checkPrime()
  File "C:/Python27/Lib/idlelib/primeChecker.py", line 5, in checkPrime
    if primeASK.isdigit():
AttributeError: 'int' object has no attribute 'isdigit'

2 个答案:

答案 0 :(得分:2)

您正在尝试将字符串除以整数,因此,您需要先将输入解析为int

def checkPrime():
    primeASK = raw_input('''Please enter a prime number.''')
    print 'Your Number is %s.' %(primeASK)

    if primeASK.isdigit():
        primeASK = int(primeASK)
        number = 1

另外,number + 1应为number += 1

答案 1 :(得分:2)

primeAsk是一个字符串,因为它是raw_input()返回的内容。您的错误是您将String除以整数。