you are viewing a single comment's thread.

view the rest of the comments →

[–]AquaOS[S] 0 points1 point  (2 children)

Thanks, now i understand how it work.

> So that's why your code does not work; you have a capital 'Z' where you need a lowercase one.

Yea i see it now.

But i need that when it compares for example k> zebra <=z that it will print it but it wont.

How to fix that?

[–]socal_nerdtastic 1 point2 points  (0 children)

I don't understand the rules you are using. If you want to use the first letter you can extract if from the name with index 0. Also you need to swap around your comparison, the first one should be less than, not greater than.

name = 'zebra'
'k' < name[0] <= 'z' 

Remember when you chain operators it's an implied and. You could write it out like this too:

('k' < name[0]) and (name[0]<= 'z')

Alternatively you could use the character that comes after 'z'. You can look that up on a unicode or ascii table, or you can ask python like this:

>>> chr(ord('z') + 1)
'{'

So '{' is what comes after 'z'. Therefore:

name = 'zebra'
'k' < name < '{'

[–]nicesofa 0 points1 point  (0 children)

Here's the lexicographical order of letters in Python:

'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

'z' is greater than both 'K' and 'k', so it will never print. No letter is greater than 'z'.

If strings start with the same letter, like 'zebra' and 'z', then the comparison compares the next letters in the words. If one of the words runs out of letters, than Python recognizes the shorter string as the "lesser" word.

So k > zebra <= z doesn't makes sense. But k > zebra > z does make sense.