TypeError:range()整数结束参数expect,得到str在Python中

时间:2014-02-07 08:36:46

标签: python range

我是新手并尝试使用python。 我在python中编写了一个程序,当我执行时,我得到了错误。有人可以帮帮我吗?

程序:

first = raw_input("Enter the first no:")
second = raw_input("Enter Second no:")
if first <= 0:
        print "Enter a valid number"
if second <= first:
        print "Sencond number should be greater than first"
for x in range(first,second):
        for i in range(2, i):
                if x % i == 0:
                        j = x/i
                        print x,  " is not a prime no"
                        print "%d = %d*%d" % (x, i, j)
                        break;
                else:
                        print x, " is not a prime number"

输入和错误:

Enter the first no:1
Enter Second no:9
Traceback (most recent call last):
  File "today1.py", line 7, in <module>
    for x in range(first,second):
TypeError: range() integer end argument expected, got str.

提前谢谢。

1 个答案:

答案 0 :(得分:6)

raw_input()返回一个字符串。

试试这段代码:

    first = int(raw_input("Enter the first no:"))
    second = int(raw_input("Enter Second no:"))

您确定for i in range(2, i):不应该是for i in range(2, x):吗?