[Tutor] Using a list
The tutorial I'm using is discussing list, tuples, and dictionaries. An exercise at the end wants me to do this: Write a Character Creator program for a role-playing game. The player should be given a pool of 30 points to spend on four attributes: Strength,Stamina, Wisdom, and Dexterity. The player should be able to spend points from the pool on any attribute and should also be able to take points from an attribute and put them back into the pool. I'm wondering if I'm doing this right or is their a different more practical way of accomplishing this. I tend to over complicate things sometimes. This is how I've started it and any help pointing me in the right direction or of a different way of doing this will be apprectiated. Thanks Doug: #user menu def menu(): choice=None print'''\n 0. Quit 1. Display attributes 2. Add points to attributes 3. Subtract opints from attributes''' --not active yet print'Remaining points=',points while choice not in('0','1','2','3 '): choice=raw_input('\nWhat would you like to do? Enter number of option.\n') return choice #variables used dexterity=0 strength=0 stamina=0 wisdom=0 allot=None creating=True change=0 points=30 #list of attributes and their values attributes=['dexterity=',dexterity ,'strength=',strength, 'stamina=',stamina, 'wisdom=',wisdom] #creation loop while creating: choice=menu() if choice=='0': print '\nThank you for using character creator.' end=raw_input('\n Press enter to end.') creating=False elif choice=='1':#prints out list of attributes and their values for entry in attributes: print entry, elif choice=='2': allot=raw_input('''What attribute would you like to change? Enter dex for dexterity, str for strength, etc. ''').lower() if allot=='dex': change=int(raw_input('How many points do you wish to allot? ')) attributes[1]=dexterity=+change points=points-change if allot=='str': change=int(raw_input('How many points do you wish to allot? ')) attributes[3]=strength=+change points=points-change if allot=='stam': change=int(raw_input('How many points do you wish to allot? ')) attributes[5]=stamina=+change points=points-change if allot=='wis': change=int(raw_input('How many points do you wish to allot? ')) attributes[7]=wisdome=+change points=points-change ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tinkering with Tkinter
--- On Tue, 5/26/09, W W sri...@gmail.com wrote: From: W W sri...@gmail.com Subject: Re: [Tutor] Tinkering with Tkinter To: Doug Reid rnrcr...@yahoo.com Cc: Tutor@python.org Date: Tuesday, May 26, 2009, 11:42 AM On Mon, May 25, 2009 at 7:45 PM, Doug Reid rnrcr...@yahoo.com wrote: The following code and it's explanation doesn't seem to work: 1. from Tkinter import * 2. tk = Tk() 3. btn = Button(tk, text=click me) 4. btn.pack() In line 1, we import the contents of the Tk module, so we can use them—the most useful of these is Tk, which creates a basic window to which we can then add things. After you type in line 2, that window will suddenly appear on the screen. In line 3, we create a new Button and assign it to the variable btn. The button is created by passing the tk object as a parameter, along with a named parameter with the words ‘click me’. Nothing appears on the screen after I enter line 2...I can type in the code in the editor and double click after saving the file and it does, but not from the shell, how can that be fixed or am I doing something wrong? Works for me! What is your os/python verision? Are you working from Idle? AFAIK, IDLE is written in Tk and so it does not so much like Tkinter programs in it's interactive shell. If you're on windows, try typing cmd in the run dialog and then type python at the prompt, then try the example. HTH, Wayne +++Thanks for the help. I tried it from the command line and it worked fine. Doug ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Can't print a string, gives me syntax error
--- On Mon, 5/25/09, xbmuncher xboxmunc...@gmail.com wrote: From: xbmuncher xboxmunc...@gmail.com Subject: [Tutor] Can't print a string, gives me syntax error To: tutor@python.org tutor@python.org Date: Monday, May 25, 2009, 5:48 PM I ran this in IDLE: t = 'hi' print t SyntaxError: invalid syntax (pyshell#3, line 1) I've also tried this as sample.py : import string text = 'hello world' print text It gives me a syntax error on print text line What's going on? I'm guessing, though I'm not that experienced with Python yet, you maybe using Python 3.0. In this new version, you have to include () with print statements. You didn't have to in 2.X versions. So try: text='Hello Wold' print (text) That's my guess, if you are using Python 2.x then I don't know:) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tinkering with Tkinter
The following code and it's explanation doesn't seem to work: 1. from Tkinter import * 2. tk = Tk() 3. btn = Button(tk, text=click me) 4. btn.pack() In line 1, we import the contents of the Tk module, so we can use them—the most useful of these is Tk, which creates a basic window to which we can then add things. After you type in line 2, that window will suddenly appear on the screen. In line 3, we create a new Button and assign it to the variable btn. The button is created by passing the tk object as a parameter, along with a named parameter with the words ‘click me’. Nothing appears on the screen after I enter line 2...I can type in the code in the editor and double click after saving the file and it does, but not from the shell, how can that be fixed or am I doing something wrong? Thanks Doug ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi everyone....thanks for the help
Thank you all for the help. I believe I understand now, and think this will be a great group to learn from. Doug ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Hi everyone
Hi, I'm teaching myself Python mainly for to use as a hobby. I'd like to do graphical programs eventually and maybe some simple graphic games. I feel I'm doing well with the tutorial I'm using but it would be nice to have some real people to ask questions and opinions, so on that note, I'm having a little trouble understanding the following code. I grasp the basics of what it does but am wondering if someone could explain it in simpler terms.. In the tutorial, I'm using Tuples and there is a Word Jumble game given to show how this can be used. A tuple of words is created: WORDS = (python, jumble, easy, difficult, answer, xylophone) correct=word Then a variable that will hold the jumbled word is defined: jumble=' ' Ok, then a variable: word=random.choice(WORDS) is created to pick a random element from WORDS. I get this part. Now here is the code I'm having trouble following: while word: position = random.randrange(len(word)) jumble += word[position] word = word[:position] + word[(position + 1):] position = random.randrange(len(word)). This will create a starting point for the program to pick a letter out of a word at random using the length of the word as a range I think. Like if the word is 'python' and since it has 6 letters, random.randrange(len(word)) will pick a random starting point such as the letter 'y' for example. It's gonna be inside a while loop, so the next time it runs, 'python' will be 'pthon' and the random range will be 5 letters and on down till there is no more letters, is this right? Ok, so everyone of the letters that was 'extracted' from the word 'python' will be put into the variable 'jumble' that was defined as empty. This is the part that troubles me: word = word[:position] + word[(position + 1):] I know basically it is creating a new string through the while loop for extracint letters. But I don't feel I understand it fully, The expression is confusing to me. Here is an excerpt from the tutorial that explains it and confuses me more: The next line in the loop, word = word[:position] + word[(position + 1):] creates a new version of word minus the one letter at position position. Using slicing, the computer creates two new strings from word. The first slice, word[:position], is every letter up to, but not including, word[position]. The next slice, word[(position + 1):], is every letter after word[position]. These two string are joined together and assigned to word, which is now equal to its old self, minus the one letter word[position]. Can someone explain this in simpler terms? I'm sorry this is so lengthy for my first post:) Any help will be appreciated Thanks, Doug ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor