[Tutor] Hooray! I finished the 'Learning Python for absolute beginners'
Ok, it's not a big deal, but once I learned enough I went off on a few tangents to create programs of my own design. Since I had to return the book to the public library, I finally got back to finishing the last chapter. Since the book is not current, I took the time to decipher the differences and eventually figured out the final project creating an asteroids game using the latest livewires. I would eventually like to use pygame, but for now I'm content that it works. I know there have been some questions in the past about errors, so this is simply being submitted as a placemarker for those working through the same book. I'm really glad I decided to learn Python! I also figured out how to configure Eclipse IDE to recognize modules. Apparently in the preferences for each 'project' is a separate PYTHONPATH.. which is not to be confused with sys.path. Adding the appropriate folders to the preferences allows the user to browse through a module i.e. after writing: 'from livewires import games' typing: 'games.' yields a drop down menu of all methods etc very convenient when trying to follow older tutorials of older packages. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hooray! I finished the 'Learning Python for > absolutebeginners'
"Alan Gauld" wrote: > I assume you have already installed the PyDev plugin? > > I'm currently reading a book on Eclipse (Eclipse Distilled) which has > turned up many settings and tricks that I was unaware of. It is a > powerful tool but like all such it takes a lot of learing to get the most > from it. > > Alan G. > Sorry Alan, I am being too vague in my writing. My instance of Eclipse was installed as part of Python(x,y) and included the Pydev plugin. I was fortunate as a beginner programmer, that someone else figured out how to configure Eclipse with the PyDev plugin. Using other IDE installed as part of Python(x,y) i.e. Spyder, gave me insight into what I was missing with Eclipse. I'm missing a lot, I mean simply missing out on one aspect of Eclipse. Tim G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Creating class instances through iteration
I came across a situation where what I thought I wanted to do was to create a class that was spawned from data in a .csv file. Where column 1 was the name I wanted to use for each instance. I had it all figured out and working except for how to write a statement where the left hand side could be a changing identifier. All I could figure out was how to create the same instance 50 times albeit in different ways. For example each row would look like [name, value1, value2, value3], I planned on passing the three values as a tuple The code would simply be: for row in csvimport: tuple = (row[1],row[2],row[3]) instancename = Classname(tuple) How could I create different instances inside an iteration loop for each row ? Is it possible to change the name through modification of self attribute (wait is self an attribute?) Are cats sleeping with dogs here or what? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Difficulty with csv files - line breaks
What I'm trying to do is store a bunch of information into a .csv file. Each row will contain a date, webpage, etc of a job application. My difficulty is that it seems something I am doing is not recording the line breaks. I've read that \r\n are default in the csv module but so far I can not seem to use them successfully..So every time I enter a new list of data it just gets appended to the end of the last row in the .csv file. It should read: "date", "company", "title", "site", "site url", "ref_ID" "date", "company", "title", "site", "site url", "ref_ID" but instead it does this: "date", "company", "title", "site", "site url", "ref_ID""date", "company", "title", "site", "site url", "ref_ID" and so forth Here's the segment of code responsible for my troubles. import csv date = raw_input("Enter the date applied: ") company = raw_input("Enter the company applied to: ") job_title = raw_input("Enter the job title: ") site = raw_input("Enter the website used: ") site_url = raw_input("Paste the URL here: ") ref_ID = raw_input("Enter the reference ID: ") entry_list = [date, company, job_title, site, site_url, ref_ID] print "Are you sure you want to add\n", for entry in entry_list: print entry print "to the file?" answer = yes_and_no() if answer == "y": append_file(entry_list,filename) def append_file(list,filename): text_file = open(filename, "a") writer = csv.writer(text_file, quoting=csv.QUOTE_NONNUMERIC) writer.writerow(list) text_file.close() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difficulty with csv files - line breaks
> Date: Tue, 24 Nov 2009 15:01:38 -0800 > From: Albert Sweigart > To: tutor@python.org > Subject: Re: [Tutor] Difficulty with csv files - line breaks > Message-ID: > <716dd5b60911241501y57db5c62r358b1a9859a3a...@mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > Tim, > > I've checked your code and it seems to work as far as using newlines > for the line terminator. The default line terminator is \r\n, which > might not show up correctly in some text editors. > > Otherwise, try checking to see if you've specified a blank line for > the line terminator. You can set it explicitly when you create your > csv.writer: > > writer = csv.writer(text_file, quoting=csv.QUOTE_NONNUMERIC, > lineterminator='\r\n') > > > -Al > You should check out my free beginner's Python book here: > http://inventwithpython.com > > > -- Al, That helped out a lot. I had worked around this problem by writing a "\n" after writing the csv information. text_file.write("\n") Setting the lineterminator explicitly fixes it and cleans up my code. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python equivalent to Matlab keyboard function
This is probably an easy one. When I was writing Matlab m-files, I really enjoyed the ability to stop the code to check how values were being stored or to 'step' into a function with the keyboard function. I have numerous 'environments'? as part of Python (x,y) including IDLE, Eclipse, and Spyder. I know that Spyder is very Matlab esque, but I prefer to use Eclipse as my programs up to this point have not been mathematics based. To do what I want In Spyder I paste a portion of my program and click the run icon. The interactive console then allows me to proceed through the next lines manually so that I can get real-time feedback. This is really helpful in understanding what I'm doing with OOP as I'm learning from a book and would like to follow along just like it is written which is usually a few lines at a time. Is there a way in Eclipse to do the same thing so I am not switching between environments? When I try the same thing it just terminates at the end of the code. (I am following the learning python for absolute beginners book. I love Python btw, it is very easy to use IMO, and has the muscle to do some really cool things.) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python equivalent to Matlab keyboard function
> > Message: 6 > Date: Fri, 4 Dec 2009 11:57:45 -0500 > From: Kent Johnson > To: Tim Goddard > Cc: tutor@python.org > Subject: Re: [Tutor] Python equivalent to Matlab keyboard function > Message-ID: > <1c2a2c590912040857nacae64jcd9feab87af58...@mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > On Fri, Dec 4, 2009 at 11:10 AM, Tim Goddard > wrote: >> This is probably an easy one. >> >> When I was writing Matlab m-files, I really enjoyed the ability to >> stop the code to check how values were being stored or to 'step' into >> a function with the keyboard function. >> >> I have numerous 'environments'? as part of Python (x,y) including >> IDLE, Eclipse, and Spyder. >> >> I know that Spyder is very Matlab esque, but I prefer to use Eclipse >> as my programs up to this point have not been mathematics based. >> >> To do what I want In Spyder I paste a portion of my program and click >> the run icon. ?The interactive console then allows me to proceed >> through the next lines manually so that I can get real-time feedback. >> This is really helpful in understanding what I'm doing with OOP as I'm >> learning from a book and would like to follow along just like it is >> written which is usually a few lines at a time. >> >> Is there a way in Eclipse to do the same thing so I am not switching >> between environments? ?When I try the same thing it just terminates at >> the end of the code. > > I'm not really sure what you are doing in Spyder but you should be > able to do what you want in PyDev using the debugger or the > interactive console. > > Kent > Right, My problem is I can't run the code in the interactive console. I'm sure this is a configuration issue in eclipse. Copy and pasting the code into an interactive console window results in a bunch of errors that aren't really there. Running the code normally results in termination once it reaches the end. IDLE and Spyder are meant for python so there is no setup, and I can interact with the program as it remains 'running'. I guess I'll have to spend some time reading more about eclipse and getting the interactive feature working. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question : Creating cribbage game
> Message: 2 > Date: Mon, 7 Dec 2009 02:30:30 -0400 > From: Christopher schueler > To: > Subject: [Tutor] Question : Creating cribbage game > Message-ID: > Content-Type: text/plain; charset="iso-8859-1" > > > My name is Chris Schueler and i am having some troubles with my Python > programming > > > > Our current project is to create the game of cribbage from scratch. > > The only problem is we are not allowed to use classes, only user-defind > functions and arrays. I was wondering if anybody could give me tips or > pointers on adding codes or modifying some of my program > > > > Here is my Program so far > > I will also include a .py file of it incase this doesnt look legible > > > > from random import* > > > > > def DisplayTitle(): > print > print "Welcome to Tech-Sauve Cribbage" > print "" > print " Insctructions " > print "" > print "1) Only played with two players (for now) " > print "2) The program starts with a full deck of 52 cards" > print "3) Deals out 6 cards to each player with a Suit letter" > print "4) Then asks each player what 2 cards they want to discard to the > crib" > print "5) Then the program saves the crib in a temporary deck" > print "6) Players start showing cards to get an ammount equal to 31" > print "7) Once all the cards have been played, program counts the score" > print "8) Then the program will count all possible scores in each hand" > print " And it will add the players points to their total score" > print "9) First player to reach a score of 121 wins the game" > #Gets players names > def GetPlayer1(): > print > Player1 = str(raw_input("Player 1's name ")) > return Player1 > def GetPlayer2(): > print > Player2 = str(raw_input("Player 2's name ")) > return Player2 > #Building the deck > def Build_Deck(): > for R in range (0,52): > cardnumb = numbers[R] > cardsuit = suits[R] > card = str(numbers[R])+str(suits[R]) > Deck.append(card) > return Deck,numbers,suits,card,cardnumb,cardsuit > > > #Variables Needed > numbers = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]*4 > suits = ["H","C","S","D"]*13 > suits.sort() > Deck = [] > P1hand = [] > P2hand = [] > Crib = [] > Cribcard = [] > Cribsuit = [] > P1_score = 0 > P2_score = 0 > Winner = 121 > ele = 52 > Deck,numbers,suits,card,cardnumb,cardsuit = Build_Deck() > for X in range(0,6): > Y = randint(0,ele) > draw = Deck[Y] > P1hand.append(draw) > Deck.pop(Y) > ele -= 1 > for X2 in range (0,6): > Y1 = randint(0,ele) > draw2 = Deck[Y1] > P2hand.append(draw2) > Deck.pop(Y1) > ele -= 1 > print > Top = randint(0,47) > Topcard = Deck[Top] > print > for count in range(0,2): > print P1hand > print > option = str(raw_input("Player 1,what CARD would you like to add to the > crib? CARDS 1 thru 6 ")) > if option == "1": > Crib.append(P1hand[0]) > P1hand.pop(0) > elif option == "2": > Crib.append(P1hand[1]) > P1hand.pop(1) > elif option == "3": > Crib.append(P1hand[2]) > P1hand.pop(2) > elif option == "4": > Crib.append(P1hand[3]) > P1hand.pop(3) > elif option == "5": > Crib.append(P1hand[4]) > P1hand.pop(4) > elif option == "6": > Crib.append(P1hand[5]) > P1hand.pop(5) > print > for c2 in range(0,2): > print P2hand > print > option1 = str(raw_input("Player 2, what CARD would you like to add to the > crib? CARDS 1 thru 6 ")) > if option1 == "1": > Crib.append(P2hand[0]) > P2hand.pop(0) > elif option1 == "2": > Crib.append(P2hand[1]) > P2hand.pop(1) > elif option1 == "3": > Crib.append(P2hand[2]) > P2hand.pop(2) > elif option1 == "4": > Crib.append(P2hand[3]) > P2hand.pop(3) > elif option1 == "5": > Crib.append(P2hand[4]) > P2hand.pop(4) > elif option1 == "6": > Crib.append(P2hand[5]) > P2hand.pop(5) > > print Deck > print "The TOP CARD is ",Topcard > print "Player 1's Hand is ",P1hand > print "Player 2's Hand is ",P2hand > print "The 4 cards in the Crib are ",Crib > Unfortunately I had to read a few wiki pages of cribbage first, so my understanding of the game is weak. My suggestions: Start with an outline of play (more to help us understand cribbage) >From my quick lesson, it sounds like you have so far: Get player names (two players) Create deck Ask player which cards to put in crib So for what you have now here are some suggestions: You are creating variables "numbers" and "suits" in your global namespace. Then you use them in your Build_Deck function which is fine, but then you are returning them at the end of the function, overwriting the original variable definition. I don't think it would mess up your code but it is messy. I also don't see where you
Re: [Tutor] What books do you recommend?
I just finished Michael Dawson's Python Programming for the absolute beginner. I thought it was pretty good, with only a few minor nit picks. My programming background was limited to MATLAB and some Visual Basic scripting for excel, access etc making me the target audience. I liked the examples, and by the end has you put together a simple asteroid-style game (with livewires and pygame). If my C class back in undergrad had promised I'd be making some shooting games in the end perhaps I'd have kept with it and gone into computer science or something. I also encountered a previous posters problem with the pizza not falling down. If you use the latest version of livewires the Sprite class does not accept velocity arguments and you have to invoke a separate Mover class to make it move. Considering I had no experience with python before this book, that I was able to open the games module and determine what changed says a lot about what can be learned from a 'beginners' book. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Sound problems
I'm still learning, and this may be better served on a pygame mailing list but I thought I'd try here first. I'm following the python programming for absolute beginners which uses livewires and pygame to do some simple games. My difficulty comes from not using the module versions used in the book and using the latest online offerings. The book creates a simple program to play music and sound in a console environment. initially the code was (shortened for brevity): from livewires import games # load a sound file missile = games.load_sound("missile.wav") #load the music file games.load_music("theme.mid") choice == None while choice != "0": print """ 1 - play missile sound 2 - play music """ choice = raw_input("Choose your selection: ") if choice == "1": missile.play() elif choice == "2": games.play_music() Which gives an error because the load_music is not part of the latest games.py module. So after reading through games.py I discovered that most of it is a pass through to pygames. Also the sound does not play. My modified code which plays music but still not sound is: from livewires import games # If I don't keep the above line, I get an error that pygame.mixer is not initialized properly import pygame # load a sound file missile = pygame.mixer.Sound('pickup.wav') # load the music file pygame.mixer.music.load("theme.mid") if choice == "1": missile.play(3) # loop .wav three times. print "Playing missile sound" elif choice == "2": pygame.mixer.music.play() print "Playing theme music" My question is why won't the sound play in the console? When I use the same code in a program with a screen, it plays normally. Thanks in advance! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor