1

So i'm trying to do this python program that will print if a word is in alphabetical order or not. I'm new to python. I don't really know much. I tried to gather up this program but it has some errors.

////Code////

 b = "hello world"
 words = b.split()
 i = b.split()
 # sort the list
 words.sort()
 for word in i:
 i.append(word)
 if i == words:
 print(True)
 else:
 print(False)

///Error///

this is the error message Line 2: Syntax Error: bad input (' ')

Ghezlan
  • 37
  • 2
  • 11

1 Answers1

0

A few things to note:
1. You want to sort the word in the same case. If you do ord("H") and ord("e") you'll see that Python considers "H" to be before "e".
2. You can directly compare the sorted output, you don't need to do it character by character.
3. You don't need that final if statement.

words = "Hello world ace"
words = [word.lower() for word in words.split()]
for word in words:
    print(sorted(word) == list(word))
Batman
  • 8,571
  • 7
  • 41
  • 80
  • how can accomplish this in your answer "The word must be terminated by a blank, so that the automaton will know when it has read all the characters" ?! – Ghezlan Mar 26 '17 at 04:20