• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
JacobChristopher

Python 3.3.0 sort() method not working?

10 posts in this topic

Hey guys I'm new here and I'm practically brand new to programming. I knew how to do a (very small) handful of things in actionscript like 7-8 years ago but I'm just now finally getting back into programming. After browsing theses forums and others I decided that Python was the best for me to start with. Since this was the most helpful forum in that decision, I ended up signing yesterday. And today I finally have a problem that I could use some help with.

I'm following the inventwithpython.com book and got stuck on chapter 11:
[url="http://inventwithpython.com/chapter11.html"]http://inventwithpyt.../chapter11.html[/url]

A bit past halfway in the "The sort() list method" section it says to try typing the following into the shell:

>>> spam = [5, 'bat', 3, 1, 4, 'cat', 2, 'ape']
>>> spam.sort()
>>> spam
[1, 2, 3, 4, 5, 'ape', 'bat', 'cat']


I fully understand what the sort() method does, but it is giving me this error:

[color=#ff0000]Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
spam.sort()
TypeError: unorderable types: str() < int()[/color]

This happens right after I enter spam.sort()


I've tried Googling this and using the search function on forums but I can't find anything.
Any help would be greatly appreciated! Thanks!


EDIT: Now that I'm running the Bagels game I can see that the sort() method isn't working in that either. But it's not giving me any errors when running or playing the game. Edited by JacobChristopher
1

Share this post


Link to post
Share on other sites
That is happening because Python 3.x changed sort behavior. Now it can not sort elements with different types.
It kinda makes sense because what does it mean - to compare integer with string? Which is greater 1 or "1"? Try this yourself in Python - write print 1 < "1". Do you expect to see False ?
2

Share this post


Link to post
Share on other sites
Thanks for the quick reply. So is there a workaround I should learn or should I just skip this and continue on learning? Being completely new to this, I have no idea how important or not important sort() is/was.
0

Share this post


Link to post
Share on other sites
I suppose you could sort the list in two steps, i.e. split it into two, one with integers and the other with the strings, sort these two separately, and concatenate them back with the integers first. That said, I don't see why an integer should compare lower than a string, it's a completely arbitrary decision and it's good that Python no longer assumes this behavior.
1

Share this post


Link to post
Share on other sites
For Python 2.x built-in types it is described here: http://docs.python.org/2/library/stdtypes.html#comparisons
[quote]CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.[/quote]
So basically values are ordered first by their type names - "int" < "string". Edited by Martins Mozeiko
0

Share this post


Link to post
Share on other sites
Python 3 has a cool new feature where you can supply a key to the 'sort' member and 'sorted' function. For example, to sort your example as a string you can do:
spam.sort(key=str)

Where the key expects a function that takes one argument and returns a value. A more advanced usage I ran into the other day was to sort a list of dictionaries, I wanted to sort by a key in the dictionary. Example:

[source lang="python"]a=[{'name':'Joe','cash':96},{'name':'Bob','cash':77},{'name':'Jane','cash':3},{'name':'Jill','cash':103}]
a.sort(key=lambda x: x['cash'],reverse=True)
print(a)[/source]

Since you are learning python I'll explain just a bit more.

Dictionary: Things between {} are a dictionary (key-value pair data structure). Value in a dictionary can be referenced easily with a['key name'] like in line 2.

Named arguments: on line 2 of my code, I use the named arguments 'key' and 'reverse'. reverse simply means I want to sort in reverse order (when True).

Lambda: Lambda functions (in simple to understand terms) are nameless functions that implicitly return a value. Keep in mind, key was expecting a function 'pointer', so you can see that when you use lambda it returns a function pointer. I read that using lambda in this situation has a performance hit compared to using an equivalent function, but that's just something to note rather than overly concern yourself with.
0

Share this post


Link to post
Share on other sites
[quote name='GMuser' timestamp='1352368687' post='4998811']
Python 3 has a cool [b]new [/b]feature where you can supply a key to the 'sort' member and 'sorted' function. For example, to sort your example as a string you can do:
spam.sort(key=str)
[/quote]
Not new. [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] My python 2.7 has it at least. Edited by tufflax
0

Share this post


Link to post
Share on other sites
[quote name='JacobChristopher' timestamp='1352350666' post='4998744']
So is there a workaround I should learn or should I just skip this and continue on learning?[/quote]
What you should do, is install the correct version of Python for your tutorial (or find a tutorial that is meant for Python 3.x).

There are enough differences between Python 2.x and 3.x to make your learning process a nightmare if you persist with the current tutorial/Python mismatch.
2

Share this post


Link to post
Share on other sites
[quote name='GMuser' timestamp='1352368687' post='4998811']
Python 3 has a cool new feature where you can supply a key to the 'sort' member and 'sorted' function. For example, to sort your example as a string you can do:
spam.sort(key=str)

Where the key expects a function that takes one argument and returns a value. A more advanced usage I ran into the other day was to sort a list of dictionaries, I wanted to sort by a key in the dictionary. Example:

[source lang="python"]a=[{'name':'Joe','cash':96},{'name':'Bob','cash':77},{'name':'Jane','cash':3},{'name':'Jill','cash':103}]
a.sort(key=lambda x: x['cash'],reverse=True)
print(a)[/source]

Since you are learning python I'll explain just a bit more.

Dictionary: Things between {} are a dictionary (key-value pair data structure). Value in a dictionary can be referenced easily with a['key name'] like in line 2.

Named arguments: on line 2 of my code, I use the named arguments 'key' and 'reverse'. reverse simply means I want to sort in reverse order (when True).

Lambda: Lambda functions (in simple to understand terms) are nameless functions that implicitly return a value. Keep in mind, key was expecting a function 'pointer', so you can see that when you use lambda it returns a function pointer. I read that using lambda in this situation has a performance hit compared to using an equivalent function, but that's just something to note rather than overly concern yourself with.
[/quote]

Thanks for this! I don't 100% understand this yet, but I've written it down in my notes. It'll click eventually. That's what I'm starting to realize is happening. I'll understand half of what I'm learning right away, and the other half I'm like "ummmmmmm, I kinda get it but not all the way..." and then all of a sudden when a very similar function is being defined I get an "a-ha!" moment. Then I go back to what I was having trouble with and it's no sweat. As lame as it sounds, this is very exciting to me haha.

[quote name='swiftcoder' timestamp='1352396658' post='4998939']
[quote name='JacobChristopher' timestamp='1352350666' post='4998744']
So is there a workaround I should learn or should I just skip this and continue on learning?[/quote]
What you should do, is install the correct version of Python for your tutorial (or find a tutorial that is meant for Python 3.x).

There are enough differences between Python 2.x and 3.x to make your learning process a nightmare if you persist with the current tutorial/Python mismatch.
[/quote]

The inventwithpython.com book says to download Python 3.1. I didn't think that there would be too much of a difference between that and 3.3, but looks like I was wrong! The only 3.1 version they have for download is 3.1.5 so I'll give that a try. Thanks! Edited by JacobChristopher
0

Share this post


Link to post
Share on other sites
[quote name='JacobChristopher' timestamp='1352427341' post='4999107']
The inventwithpython.com book says to download Python 3.1. I didn't think that there would be too much of a difference between that and 3.3, but looks like I was wrong! The only 3.1 version they have for download is 3.1.5 so I'll give that a try. Thanks![/quote]
Actually you are correct on that. I hadn't looked at the book previously, and while it does (strongly) direct you to download Python 3.x, it looks like they haven't actually finished updating the book for 3.x.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0