TypeError: 'str'对象不能被解释为整数

时间:2021-04-18 20:25:35

I don't understand what the problem is with the code, it is very simple so this is an easy one.

我不明白代码有什么问题,它很简单,所以这很简单。

x = input("Give starting number: ")
y = input("Give ending number: ")

for i in range(x,y):
 print(i)

It gives me an error

它给了我一个错误

Traceback (most recent call last):
  File "C:/Python33/harj4.py", line 6, in <module>
    for i in range(x,y):
TypeError: 'str' object cannot be interpreted as an integer

As an example, if x is 3 and y is 14, I want it to print

举个例子,如果x = 3 y = 14,我想打印出来

Give starting number: 4
Give ending number: 13
4
5
6
7
8
9
10
11
12
13

What is the problem?

这个问题是什么?

6 个解决方案

#1


8  

A simplest fix would be:

最简单的解决办法是:

x = input("Give starting number: ")
y = input("Give ending number: ")

x = int(x)  # parse string into an integer
y = int(y)  # parse string into an integer

for i in range(x,y):
    print(i)

input returns you a string (raw_input in Python 2). int tries to parse it into an integer. This code will throw an exception if the string doesn't contain a valid integer string, so you'd probably want to refine it a bit using try/except statements.

输入返回字符串(Python 2中的raw_input)。int试图将其解析为整数。如果字符串不包含有效的整型字符串,那么该代码将抛出一个异常,因此您可能希望使用try/except语句对其进行一些细化。

#2


0  

I'm guessing you're running python3, in which input(prompt) returns a string. Try this.

我猜您在运行python3,输入(提示符)返回一个字符串。试试这个。

x=int(input('prompt'))
y=int(input('prompt'))

#3


0  

You have to convert input x and y into int like below.

你必须将输入x和y转换成如下的整数。

x=int(x)
y=int(y)

#4


0  

You will have to put:

你必须写:

X = input("give starting number") 
X = int(X)
Y = input("give ending number") 
Y = int(Y)

#5


0  

Or you can also use eval(input('prompt')).

或者也可以使用eval(input('prompt'))。

#6


0  

x = int(input("Give starting number: "))
y = int(input("Give ending number: "))

P.S. Add function int()

注:添加int()函数

#1


8  

A simplest fix would be:

最简单的解决办法是:

x = input("Give starting number: ")
y = input("Give ending number: ")

x = int(x)  # parse string into an integer
y = int(y)  # parse string into an integer

for i in range(x,y):
    print(i)

input returns you a string (raw_input in Python 2). int tries to parse it into an integer. This code will throw an exception if the string doesn't contain a valid integer string, so you'd probably want to refine it a bit using try/except statements.

输入返回字符串(Python 2中的raw_input)。int试图将其解析为整数。如果字符串不包含有效的整型字符串,那么该代码将抛出一个异常,因此您可能希望使用try/except语句对其进行一些细化。

#2


0  

I'm guessing you're running python3, in which input(prompt) returns a string. Try this.

我猜您在运行python3,输入(提示符)返回一个字符串。试试这个。

x=int(input('prompt'))
y=int(input('prompt'))

#3


0  

You have to convert input x and y into int like below.

你必须将输入x和y转换成如下的整数。

x=int(x)
y=int(y)

#4


0  

You will have to put:

你必须写:

X = input("give starting number") 
X = int(X)
Y = input("give ending number") 
Y = int(Y)

#5


0  

Or you can also use eval(input('prompt')).

或者也可以使用eval(input('prompt'))。

#6


0  

x = int(input("Give starting number: "))
y = int(input("Give ending number: "))

P.S. Add function int()

注:添加int()函数