Re: [Tutor] Beginner question

2013-08-12 Thread Ciaran Mooney


On 10 Aug 2013, at 04:30, eschneide...@comcast.net wrote:

 I've been learning python from the website 'inventwithpython.com', and I'm on 
 a chapter that covers the following code:
 
 import random
 import time
 def displayIntro():
 print('You are in a land full of dragons. In front of you,')
 print('you see two caves. In one cave, the dragon is friendly')
 print('and will share his treasure with you. The other dragon')
 print('is greedy and hungry, and will eat you on sight.')
 print()
 def chooseCave():
 cave = ''
 while cave != '1' and cave != '2':
 print('Which cave will you go into? (1 or 2)')
 cave = input()
  return cave
 def checkCave(chosenCave):
 print('You approach the cave...')
 time.sleep(2)
 print('It is dark and spooky...')
 time.sleep(2)
 print('A large dragon jumps out in front of you! He opens his jaws 
 and...')
 print()
 time.sleep(2)
 friendlyCave = random.randint(1, 2)
 if chosenCave == str(friendlyCave):
 print('Gives you his treasure!')
 else:
 print('Gobbles you down in one bite!')
 playAgain = 'yes'
 while playAgain == 'yes' or playAgain == 'y':
 displayIntro()
 caveNumber = chooseCave()
 checkCave(caveNumber)
 print('Do you want to play again? (yes or no)')
 playAgain = input()
 
 I'm confused about what the line 'checkCave(caveNumber)' does and how it 
 works. I would appreciate any help with this
 
 Thank you,
 
 Eric
 

Hi Eric,

This line calls the method 'checkCave()'. This method takes the argument 
caveNumber (checkCave(caveNumber) , which is a string returned from user input 
generated in the method chooseCave().

In checkCave method, the value (caveNumber) is then used in a if/else statement 
to compare with a random integer (parsed to a string str(frindlyCave))  to 
determine whether you will be eaten or given treasure ;)

Hope that was of some help. 

Ciaran 
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] invalid literal for int error in Game loop

2013-01-05 Thread Ciaran Mooney


On 4 Jan 2013, at 19:20, Francesco Loffredo f...@libero.it wrote:

 Ciaran Mooney wrote:
 Thanks for the feedback.
 
 Steve, If I set the FPS to a default of say 30, the game seems to run at 
 this default FPS=30 regardless of the key pressed in the function.
 
 Dave, If I remove the the default value at the start of the function and add 
 it to elif in the loop I get the following error:
 
 
 Traceback (most recent call last):
   File /Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger 
 ver 3 instructons.py, line 139, in module
 FPS = difficultyLevel()
   File /Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger 
 ver 3 instructons.py, line 50, in difficultyLevel
 return FPS
 UnboundLocalError: local variable 'FPS' referenced before assignment
 
 
 I wondered if i could set FPS to nether a string or integer and just declare 
 it by setting FPS=None but I get the following error:
 
 Traceback (most recent call last):
   File /Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger 
 ver 3 instructons.py, line 301, in module
 mainClock.tick(FPS)
 TypeError: a float is required
 
 Cheers
 Ciaran
 As far as I understood your problem, it seems that you need the difficulty 
 level to be changed at the player's will during the game. In this case, the 
 function gets called many times, maybe at every game loop. If this is true, 
 you need a global FPS value to be changed by the player's choice, not one 
 that you create inside the function.
 
 Try this:
 
 # put the line below out of the function definition and out of the game loop, 
 usually together with other default program settings
 FPS = 30  # you want the game to be beginner-friendly, don't you?
 
 def difficultyLevel():
global FPS
 windowSurface = pygame.display.set_mode((WINDOWWIDTH, 
 WINDOWHEIGHT),pygame.FULLSCREEN)
 windowSurface.fill(BACKGROUNDCOLOUR)
 drawText('LEVEL OF PLAY', font3, windowSurface, (WINDOWWIDTH/6), 
 (WINDOWHEIGHT/6)) 
 drawText('B: Your an absoulute Begineer', font3, windowSurface, 
 (WINDOWWIDTH/6)-100, (WINDOWHEIGHT/6)+100)
 drawText('M: Not Too Snazy', font3, windowSurface, (WINDOWWIDTH/6)-100, 
 (WINDOWHEIGHT/6)+150)
 drawText('H: Deathwish' ,font3,windowSurface, (WINDOWWIDTH/6)-100, 
 (WINDOWHEIGHT/6)+200)
 pygame.display.update()
 
 for event in pygame.event.get():
 if event.type == QUIT:
 terminate()
 
 if event.type == KEYDOWN:
 if event.key == ord('b'):
 FPS = 30
 elif event.key == ord('m'):
 FPS = 70
 elif event.key == ord('h'):
 FPS = 120
 
 return FPS
 
 ... then let us know.
 Hope that  helps
 
 Francesco


Thanks Francesco,
Ill give it a go and come back to u.

Ciaran
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] invalid literal for int error in Game loop

2012-12-20 Thread Ciaran Mooney
HI All,

Was hoping someone could help me with a problem I am having programming a game 
using pygame.

I am trying to return a variable with a integer value defined by user input 
from a function.

Basically the idea is to allow the player to select the level of difficulty by 
selecting from a choice of keys, each key corresponding too  the number of 
frames per second. The function code is as follows:

def difficultyLevel():
FPS = ''
windowSurface = pygame.display.set_mode((WINDOWWIDTH, 
WINDOWHEIGHT),pygame.FULLSCREEN)
windowSurface.fill(BACKGROUNDCOLOUR)
drawText('LEVEL OF PLAY', font3, windowSurface, (WINDOWWIDTH/6), 
(WINDOWHEIGHT/6)) 
drawText('B: Your an absoulute Begineer', font3, windowSurface, 
(WINDOWWIDTH/6)-100, (WINDOWHEIGHT/6)+100)
drawText('M: Not Too Snazy', font3, windowSurface, (WINDOWWIDTH/6)-100, 
(WINDOWHEIGHT/6)+150)
drawText('H: Deathwish' ,font3,windowSurface, (WINDOWWIDTH/6)-100, 
(WINDOWHEIGHT/6)+200)
pygame.display.update()


   
for event in pygame.event.get():
if event.type == QUIT:
terminate()

if event.type == KEYDOWN:
if event.key == ord('b'):
FPS = 30
elif event.key == ord('m'):
FPS = 70
elif event.key == ord('h'):
FPS = 120


return FPS

The function is then called and FPS converted too a  integer value in the main 
game loop as follows:

topScore = 0
while True:
#set up the game and all intial key and mouse movement values as false

baddies = [] #baddies as a empy list
score = 0
etc

FPS = int(difficultyLevel())

FPS is then used within the game loop  to allow for number of frames per second 
in:

mainClock.tick(FPS)

However I keep getting the following error:

ValueError: invalid literal for int() with base 10: ''


Please note I am a absolute beoingeer in programming and have had no formal 
education to date so please excuse anything I may of left out !!


Thanks
Ciaran

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] invalid literal for int error in Game loop

2012-12-20 Thread Ciaran Mooney
Thanks for the feedback.

Steve, If I set the FPS to a default of say 30, the game seems to run at this 
default FPS=30 regardless of the key pressed in the function.

Dave, If I remove the the default value at the start of the function and add it 
to elif in the loop I get the following error:


Traceback (most recent call last):
  File /Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 
3 instructons.py, line 139, in module
FPS = difficultyLevel()
  File /Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 
3 instructons.py, line 50, in difficultyLevel
return FPS
UnboundLocalError: local variable 'FPS' referenced before assignment


I wondered if i could set FPS to nether a string or integer and just declare it 
by setting FPS=None but I get the following error:

Traceback (most recent call last):
  File /Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 
3 instructons.py, line 301, in module
mainClock.tick(FPS)
TypeError: a float is required

Cheers
Ciaran
On 20 Dec 2012, at 21:07, Dave Angel d...@davea.name wrote:

 On 12/20/2012 03:51 PM, Steven D'Aprano wrote:
 On 21/12/12 07:40, Ciaran Mooney wrote:
 
 def difficultyLevel():
 FPS = ''
 
 Here you set the variable FPS to the empty string.
 
 if event.type == KEYDOWN:
 if event.key == ord('b'):
 FPS = 30
 elif event.key == ord('m'):
 FPS = 70
 elif event.key == ord('h'):
 FPS = 120
 
 These three options set FPS to an integer value. But notice that if
 the user
 does not press one of the b m or h keys, FPS never gets changed so it
 still
 has the initial value of the empty string.
 
 return FPS
 
 The function is then called and FPS converted too a  integer value in
 the main game loop as follows:
 
 FPS = int(difficultyLevel())
 
 However I keep getting the following error:
 
 ValueError: invalid literal for int() with base 10: ''
 
 There are three possible values that difficultyLevel() may return:
 
 * if the user hits the 'h' key, it returns the integer value 120;
 * if the user hits the 'm' key, it returns the integer value 70;
 * if the user hits the 'b' key, it returns the integer value 30;
 * otherwise, it returns the empty string.
 
 Three of the four values are already ints and don't need to be converted;
 the fourth cannot be converted because it is not a numeric string.
 
 To fix this, change the line FPS = '' to a default integer value,
 say, FPS = 30.
 
 
 
 
 Or even better, remove that default value and add an else clause to the
 if/elif/elif section.  That way it's all in one place, and it's more
 obvious that the return value will always be an int.
 
 And of course you can remove the int() call on the line
FPS = int( difficultyLevel() )
 becomes
FPS = difficultyLevel()
 
 BTW, when quoting an error, please include the whole traceback, not just
 one line of it.  Here it was obvious, but many times other parts will be
 very useful, or even essential.
 
 
 
 -- 
 
 DaveA
 
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Pygame problem with mac

2012-11-17 Thread Ciaran Mooney
Hi,

Was hoping u could help me. 

I can't seem to download a version of pygame that is compatible with python 3.2 
on my Mac powerbook with OS tiger. 

I only seem to he able to get pygame for python 2.7 which i have never used. 

Thanks
Ciaran


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Questions about classes

2012-11-13 Thread Ciaran Mooney
Hi,
Was hoping someone could help me. 
I have downloaded the latest pygame 1.9.1 i think) to a Mac powerbook OS 
10.4.11. 

Python 3 does not recognise pygame although python 2.7 version does 
(unfortunately have never programmed python 2.7 and don't no how).

Any help would be much appreciated.

Thanks
Ciaran



On 13 Nov 2012, at 12:57, Steven D'Aprano st...@pearwood.info wrote:

 On 13/11/12 13:49, brandon w wrote:
 
 
 1. I saw in the book an assignment written like this:
 
 class HumanBeing:
 def makeName(self, name):
   *self.name = name*
 *
 *
 Why is it not written like this?:
 
 class HumanBeing:
 def makeName(self, name):
 *  name = self.name*
 
 This creates a class called HumanBeing. It includes a method called
 makeName. Methods are very similar to functions, the differences will
 become clear further on.
 
 Methods are defined in the same way as functions: the def keyword,
 followed by the name of method, then the parameters. Each parameter
 creates a local variable, so the makeName method has two local
 variables:
 
 - self
 - name
 
 self is special. When you call the method, Python will automatically
 provide the self argument. So if you do this:
 
 fred = HumanBeing()  # create an instance of HumanBeing class
 fred.makeName(flintstone)
 
 the makeName method gets passed two arguments:
 
 - self = fred, provided automatically by Python
 - name = flintstone, provided by you
 
 
 Now, inside the body of the method, we have this:
 
 self.name = name
 
 That says:
 
 - take the argument *name* (which has value flintstone)
 - attach it to the instance *self* using the attribute called name
 
 After the line finishes executing, the instance *fred* will now have
 an attribute *name* with value flintstone.
 
 So if you later call:
 
 print(fred.name)
 
 Python will print flintstone.
 
 What if it were written the other way, as you suggested?
 
 name = self.name
 
 That goes in the opposite direction: first Python tries to look up
 an attribute called name. It probably doesn't find one, and so it
 will raise an exception and print an error message. But let's
 suppose it did find one. It then takes that value and stores it
 in the local variable name, over-writing the local variable you
 provided as an argument to the method.
 
 Then, when the method returns (either at a return statement, or
 by reaching the end of the method), the local variable is
 forgotten and no permanent change is made.
 
 
 
 2. Why use a class in the first place? What is the purpose of constructing
 a class instead of just writing a program with a bunch of functions?
 
 Classes are useful for a couple of reasons:
 
 1) Encapsulation
 
 A class keeps together related code and data. A good example comes from the
 Python built-in class list. The list class combines:
 
 - a storage area for the list data;
 - methods which operate on that list data.
 
 For example, lists have a method index. But strings also have a method
 called index. The list.index method knows how to search a list. It knows
 nothing about strings, and doesn't need to care about strings. It only
 needs to care about lists. The str.list method knows how to search a string.
 It knows nothing about lists, and only cares about strings. That makes it
 much easier to program. Instead of one giant function:
 
 
 def index(obj, value):
if obj is a string:
code for searching strings
elif obj is a list:
code for searching lists
elif obj is a tuple:
code for searching tuples
else:
raise TypeError(don't know how to index obj)
 
 instead each branch of the function gets encapsulated into a str class, a
 list class, a tuple class, and anything else that you might want to index.
 If you write a Book class, you can give it an index method without needing
 to care about lists, strings, tuples, etc.
 
 
 The other advantage of classes is:
 
 2) Inheritance
 
 With classes, you can *inherit* behaviour by creating a subclass. Say, for
 example, you want a type of list that is exactly the same as ordinary lists
 except that every time you append a value, it prints what you appended. This
 might be useful for debugging. Without inheritance, you would have to
 duplicate the entire code base for list, many hundreds or thousands of lines
 of code. But with inheritance, it just takes FOUR lines:
 
 class MyList(list):
def append(self, value):
print(appending %s % value)
super(MyList, self).append(value)
 
 
 This creates a new class called MyList, which inherits from the built-in
 list class; everything else is the same as list, except for the append
 method, which prints the value first, then calls the built-in list.append
 method.
 
 (If the super() call looks a bit mysterious, don't worry too much about it
 right now.)
 
 
 
 So between encapsulation and inheritance, classes are a powerful tool for
 programming.
 
 
 
 -- 
 Steven