0
name = int(input('>>>'))
ascending_order = name.split(', ')
ascending_order.sort()
print(ascending_order)

I am having error in line 1: int(input(">>>")

name = int(input('>>>')) ValueError: invalid literal for int()

6
  • 4
    I bet you’re typing numbers separated by a comma. If so, a comma cannot be converted to an int so do the conversion after / during the split. Commented Jun 2, 2020 at 13:15
  • 2
    If the input is a comma-delimited list of numbers, you can't have int() applied to the input. You need name = input('>>>'). You need to then convert each element of ascending_order to an int after the split. Commented Jun 2, 2020 at 13:19
  • 1
    Why are you casting the input to an integer? If you want to split something, you need to be using a string, str(input('>>>') Commented Jun 2, 2020 at 13:19
  • Split first, convert each number to int after Commented Jun 2, 2020 at 13:20
  • @I_keep_getting_downvoted: The str is implied and not needed. Commented Jun 2, 2020 at 13:21

1 Answer 1

1
  • with string conversion into integer:

    name = input('>>>')
    ascending_order = name.split(',')
    for i in range(len(ascending_order)):
         ascending_order[i] = int(ascending_order[i])
    ascending_order = sorted(ascending_order, reverse=True)
    print(ascending_order)
    
  • without string conversion into integer:

    name = input('>>>')
    ascending_order = name.split(',')
    ascending_order = sorted(ascending_order, reverse=True)
    print(ascending_order)
    
Sign up to request clarification or add additional context in comments.

2 Comments

What about something more concise like: asc = sorted([int(i) for i in name.split(‘,’)])?
You did the same thing but the technique you have used is list comprehension. and one more thing now its fine asc = sorted([int(i) for i in name.split(',')], reverse=True) .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.