gpt4 book ai didi

python - TypeError : range() integer end argument expected, 在 Python 中得到 str

转载 作者:行者123 更新时间:2023-11-28 21:21:21 25 4
gpt4 key购买 nike

我是新手,正在尝试使用 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.

提前谢谢你。

最佳答案

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): 吗?

关于python - TypeError : range() integer end argument expected, 在 Python 中得到 str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21622897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com