[英]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.