[Tutor] invalid literal for int error in Game loop

Ciaran Mooney dancingbush at gmail.com
Thu Dec 20 22:34:40 CET 2012


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 at 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 at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121220/030c53d3/attachment-0001.html>


More information about the Tutor mailing list