简体   繁体   English

即使我使用int,str类型错误

[英]str type error even though I am using int

Please help me out. 请帮帮我。 If user enters 937, program should return 973, the biggest possible number. 如果用户输入937,程序应返回973,这是最大可能的数字。

This is my code: 这是我的代码:

list = []
cont = 1
while cont < 4:
    List.append(input(("Type digit ") + str(cont ) + (" of the number: ")))
    cont = cont + 1

print(list)
mayor = int(0)
menor = int(9)
cont = int(0)
while cont < 3:i
    if list[cont] > int(mayor):
        mayor = list[cont]
    if list[cont] < menor:
        menor = list[cont]

    cont = cont + 1

cont  = 0
while cont < 3:
    if list[cont] < mayor:
    if list[cont] > menor:
        m = list[cont]
cont = cont  + 1

list1 = []
list1.append(mayor)
list1.append(m)
list1.append(menor)

print(list1)

This is the error I'm getting: 这是我得到的错误:

    if list[cont] > int(mayor):    
TypeError: '>' not supported between instances of 'str' and 'int'

Use following method 使用以下方法

list.sort()
m = list[1]

instead of this: 代替这个:

cont  = 0
while cont < 3:
    # if list[cont] < mayor:
    if list[cont] > menor:
        m = list[cont]
    cont = cont  + 1

Then try : 然后尝试:

list = []
cont = 1
while cont < 4:
    list.append(input(("Type digit ") + str(cont ) + (" of the number: ")))
    cont = cont + 1

print(list)

mayor = 0
menor = 9
cont = 0

while cont < 3:
    if int(list[cont]) > int(mayor):
        mayor = list[cont]
    if int(list[cont]) < int(menor):
        menor = list[cont]

    cont = cont + 1

list.sort()
m = list[1] # second largest

list1 = []
list1.append(mayor)
list1.append(m)
list1.append(menor)

print("".join(list1))

Input : 输入:

Type digit 1 of the number: 2
Type digit 2 of the number: 5
Type digit 3 of the number: 7

Output : 输出:

['2', '5', '7']
752

This is how I solved my issue. 这就是我解决问题的方式。

list = []
cont = 1
while cont < 4:
    list.append(input(("Ingrese el numero ") + str(cont ) + (" de la cifra: ")))
    cont = cont + 1

print("El numero ingresado es: " , list)
mayor = int(0)
menor = int(9)
cont = int(0)
while cont < 3:
    if int(list[cont]) > int(mayor):
        mayor = list[cont]
    if int(list[cont]) < int(menor):
        menor = list[cont]

    cont = cont + 1

cont  = 0
while cont < 3:
    if list[cont] < mayor:
        if list[cont] > menor:
            m = list[cont]

    cont = cont  + 1

list1 = []
list1.append(mayor)
list1.append(m)
list1.append(menor)

print("El mayor numero possible es: ",list1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不支持的操作数类型错误:str 和 int 即使类型是 str 和 str - Error of unsupported operand type for: str and int even though the type is str and str 为什么我得到:TypeError: unsupported operand type(s) for -: 'str' and 'str' 即使 dtypes 显示所有 float64 - Why am I getting: TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’ even though dtypes is showing all float64 为什么即使我没有使用任何“str”命名变量,我也会收到(“str”函数不可调用)错误? - Why am I getting ('str' function is not callable) error even though I haven't used any 'str' named variable? 为什么我会收到这种类型的错误? 类型错误:“str”和“int”的实例之间不支持“&gt;=” - Why am I getting this type error? TypeError: '>=' not supported between instances of 'str' and 'int' 我收到此错误,不支持 ** 或 pow() 的操作数类型:'str' and 'int' - I am getting this error unsupported operand type(s) for ** or pow(): 'str' and 'int' 为什么我会出错。 TypeError:+不支持的操作数类型:“ int”和“ str”? - Why am I getting error. TypeError: unsupported operand type(s) for +: 'int' and 'str'? 当我没有将 int 与其他字符串组合时,为什么在 python 中出现类型错误? TypeError:只能将str(不是“int”)连接到str - Why am I getting type error in python when I am not combining int with other string? TypeError: can only concatenate str (not “int”) to str 我正在尝试使用 xpath 使用数组单击元素,但它给了我“只能将 str(而不是”int“)连接到 str”错误消息 - I am trying to click on element using array using xpath but it gives me “can only concatenate str (not ”int“) to str” error message TypeError: 'str' object cannot be interpreted as an integer 即使我给它分配了 int(value) - TypeError: 'str' object cannot be interpreted as an integer even though I assigned it with int(value) 我有这个错误 TypeError: &#39;&lt;&#39; not supported between &#39;str&#39; and &#39;int&#39; - i am having this error TypeError: '<' not supported between instances of 'str' and 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM