在Python“str”对象中读取数字不能解释为整数

2024-09-29 01:26:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我对代码有问题。我有一个排序算法,我检查它是否有特定的输入。 作为输入,测试数1<;=d<;=100,整数0<;n<;=214748364和范围-2147483646<;=a_k<;=2147483647 输入:

3
8 5 2 6 4 1 3 2 6
8 2 4 5 6 1 3 2 6
8 1 6 2 7 3 8 4 5

我的代码:

# Bubble Sort

d = int(input()) #number of tests
#A = []
def wczytanie_liczb():
    for k in range(0,d):
        n = input() #number of digits in the string
        n = int(n)
        A = []
        for l in range(0,n):
            m = input() #fill in the array
            m = int(m)
            A.append(m)
        #print(A)
        bubble_sort(A)

def bubble_sort(A):
    i = 0
    zam = True
    while i < len(A)-1 and zam:
        j = 0
        zam = False
        while j < len(A) - 1 - i:
            if A[j] > A[j+1]:
                A[j], A[j+1] = A[j+1], A[j]
                zam = True
            j +=1
    #print(A)
        
wczytanie_liczb()

它给出了一个错误:

Traceback (most recent call last):
  File "./prog.py", line 28, in <module>
  File "./prog.py", line 9, in wczytanie_liczb
TypeError: 'str' object cannot be interpreted as an integer

Tags: ofthe代码inltnumberforinput
1条回答
网友
1楼 · 发布于 2024-09-29 01:26:11

结语。将字符串作为参数传递到range()语句中时,会引发“TypeError:'str'对象不能解释为整数”错误。要修复此错误,请确保range()语句中使用的所有值都是整数

相关问题 更多 >