[Tutor] invalid literal for int error in Game loop

Prasad, Ramit ramit.prasad at jpmorgan.com
Thu Dec 20 22:53:48 CET 2012


Ciaran Mooney wrote:
> Sent: Thursday, December 20, 2012 3:35 PM
> To: d at davea.name
> Cc: tutor at python.org
> Subject: Re: [Tutor] invalid literal for int error in Game loop
> 
> 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

If you read Dave's comment a little closer you will see that
he suggests adding an *else* block not another elif. 

(pseudocode)
if key == b:
    FPS = 30
elif key == m:
    FPS = 60 
elif key == h:
    FPS = 120
else:
    FPS = 15 # or whatever default value you want

> 
> 
> 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

The else clause prevents FPS from being anything other than a number.
You can choose to predefine FPS or just let the if/else tree take
care of defining it.

> 
> Cheers
> Ciaran
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list