不支持的opperand类型错误。我正在使用python版本3

时间:2016-06-08 10:28:29

标签: python-3.x

我正在使用PYTHON版本3.5并为PRIME NUMBER键入此代码:::

         num=input("enter number to be checked :    ")
         ctr=0
         i=1
         for i in range(1,(num/2)+1):
                 if(num%i==0):
                 ctr+=1

         if(ctr==0):
                 print(num,"    is prime")
         else:
                 print(num,"    is not prime")

并解决这个错误::::

         enter number to be checked :   5
         Traceback (most recent call last)
           file "prime.py", line 4 in (module)
                 for i in range(1,(num/2)+1):
         type error: unsupported opperand type(s) for/:str and int

1 个答案:

答案 0 :(得分:0)

使用input()时,它是一个字符串值,而不是int

更改为

num=int(input("enter number to be checked :    "))

但是,如果输入非数字值,则会生成ValueError,您可能希望构建一个支票。