1

I am getting the error:

AttributeError: 'str' object has no attribute 'string'

So here is the code:

lnumber=input("please type in numbers:")
lnumber.string().sort()
print(lnumber)

What I'm trying to do is:

  • user inputs numbers (e.g.: '2 1 4 3'); and
  • the code puts the numbers in ascending order.
3
  • 1
    input returns a str object, which doesn't have a string method - what were you expecting to happen? For the list of methods that str objects do have, see: docs.python.org/2/library/stdtypes.html#string-methods Commented Aug 17, 2014 at 11:04
  • 1
    @grc apologies, wrong link (see docs.python.org/3/library/stdtypes.html#string-methods); still no str.string, though. Commented Aug 17, 2014 at 11:11
  • what that weird string() applied to a string should do? maybe (if string is consistent) lst = [int(x) for x in lnumber.split(' ')];lst.sort() Commented Aug 17, 2014 at 11:28

1 Answer 1

1

If the user is inputting numbers in that way then you probably want to use raw_input. After that you can use .split() to get a list of the numbers and then use .sort().

Example:

myRawInput = raw_input("Enter some numbers separated by spaces")
myList = myRawInput.split()
myList.sort()
Sign up to request clarification or add additional context in comments.

Comments

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.