user9676985
user9676985

Reputation:

Issue with Input statement

I am a newbie to Python and learning lists. Here is the program I have written for sorting numbers:

def sorting(final_input):
          final_output= []
          count = 0
          length = len(final_input)
          while count < length:
                    final_output.append(min(final_input))
                    final_input.remove(min(final_input))
                    count += 1
                    if count ==  (length): 
                              break
          return(final_output)
final_input = [5,6,57,531,9,1]
print(final_input)
print(sorting(final_input))

The above program works fine and gives the following output:

[5, 6, 57, 531, 9, 1]
[1, 5, 6, 9, 57, 531]

But when I give input with input command (as follows):

def sorting(final_input):
          final_output= []
          count = 0
          length = len(final_input)
          while count < length:
                    final_output.append(min(final_input))
                    final_input.remove(min(final_input))
                    count += 1
                    if count ==  (length): 
                              break
          return(final_output)

final_input = []
while True:
          user_input = input("Enter number or quit: ")
          if user_input == "quit":
                    break
          final_input.append(user_input)
print(final_input)
print(sorting(final_input))

The above program gives the following incorrect output. It is not clear to me what needs to be updated with ‘’input’’ statement. Any help would be appreciated.

Enter number or quit: 5
Enter number or quit: 6
Enter number or quit: 57
Enter number or quit: 531
Enter number or quit: 9
Enter number or quit: 1
Enter number or quit: quit
['5', '6', '57', '531', '9', '1']
['1', '5', '531', '57', '6', '9']

Upvotes: 0

Views: 50

Answers (3)

user2910787
user2910787

Reputation:

The input with this statement

user_input = input("Enter number or quit: ")

Will be take data in form of string data type .so your input is taken a string . So sorting is NOT working

So make it int during appending

final_input.append(int(user_input))

Upvotes: 0

iBug
iBug

Reputation: 37287

As answered by kvmahesh, the return type of input() is always str. You need to convert it to int if you want numbers.

while True:
    user_input = input("Enter number or quit: ")
    if user_input == "quit":
        break

    try:
        final_input.append(int(user_input))
    except ValueError:
        print("Invalid input!")

A try...except block is good to check if user gives some invalid input (e.g. asdfg).

Also, if you just want to sort the list, you can use sorted():

final_input = sorted(final_input)

Upvotes: 1

user5777975
user5777975

Reputation:

The stdin values are always of type string. so convert using int while appending at line final_input.append(int(user_input)):

def sorting(final_input):
      final_output= []
      count = 0
      length = len(final_input)
      while count < length:
                final_output.append(min(final_input))
                final_input.remove(min(final_input))
                count += 1
                if count ==  (length): 
                          break
      return(final_output)

final_input = []
while True:
         user_input = input("Enter number or quit: ")
         if user_input == "quit":
                   break
         final_input.append(int(user_input))
print(final_input)
print(sorting(final_input))

Output:

Enter number or quit: 3
Enter number or quit: 5
Enter number or quit: 1
Enter number or quit: 2
Enter number or quit: 100
Enter number or quit: quit
[3, 5, 1, 2, 100]
[1, 2, 3, 5, 100]

Upvotes: 0

Related Questions