1

I am trying to make a game where the player is deducted .5 from a value in a ini file every time he plays. However I keep getting an error and I am not sure what to do. This is my code. Dont worry about the comments, those are for me, and I do close the while loop later. This is just a portion of the code. Btw, the code works, just not this. Thanks.

def rerun():
    import ConfigParser
    from ConfigParser import ConfigParser

    parser = ConfigParser()
    parser.read('Game.ini')

    PlrMny = parser.get('money_value', 'Amount')
    #config = ConfigParser.ConfigParser()
    configFile = open("C:\Python27\Game.ini", "w")
    #config.read(configFile)
    #valueamount = config.getfloat("section","starting_value")

    print "You will be given a $10 starting amount. Each game costs $.50 to play and is
    deducted when you input the first value."
    print "\nGetting one match gives $1 and the output is multiplied by 2 for each extra 
match."
    print "\nCurrent Amount =",PlrMny,

def gamble():
    PlrMny = parser.get('money_value', 'Amount')


    import random
    import sys
    number1 = random.randint (1, 20)
    number2 = random.randint (1, 20)
    number3 = random.randint (1, 20)
    number4 = random.randint (1, 20)
    number5 = random.randint (1, 20)



    def input():
        c = 0
        print "\n\n\n\nTry guess what five numbers the computer will guess. Type '100'
        in any of the inputs to close program prematurely"    
        print "Money =",PlrMny,
        #parser.set("money_value", "Amount",10000)
        #parser.write ('Game.ini')

        while True:

            try:
                User11 = int(raw_input( "\n\nNumber 1 : "))
                parser.set('money_value','Amount',float(PlrMny) - .5)
                parser.write (configFile)
                str(PlrMny)
                if User11 < 1:
                    print "Error"
                elif User11 == 100:
                    sys.exit()
                elif User11 > 20:
                    print "Error"
                else:
                    break
            except ValueError:
                print "Error"

This is the Error:

 Traceback (most recent call last):
  File "C:\Python27\Gamb Game.py", line 183, in <module>
    rerun()
  File "C:\Python27\Gamb Game.py", line 182, in rerun
    gamble()
  File "C:\Python27\Gamb Game.py", line 19, in gamble
    PlrMny = parser.get('money_value', 'Amount')
  File "C:\Python27\lib\ConfigParser.py", line 623, in get
    return self._interpolate(section, option, value, d)
  File "C:\Python27\lib\ConfigParser.py", line 663, in _interpolate
    if value and "%(" in value:
  TypeError: argument of type 'float' is not iterable
6
  • whats your ini file look like? Apr 3, 2013 at 5:23
  • [money_value] amount = 9.0 (on two different lines) Apr 3, 2013 at 5:26
  • 1
    @user2238780 you will get better help if you take the time to clean up your question. You need to remove unneeded code, (ie configFile = open("C:\Python27\Game.ini", "w")) and it is usually helpful to remove any code that is not relevant to the question itself. This process takes time but is a really good debugging tactic and people are more willing to help when there is less code to read
    – amccormack
    Apr 3, 2013 at 5:31
  • i am not quite sure, but maybe the parser is case sensitive?
    – mkind
    Apr 3, 2013 at 5:33
  • @amccormack actually, that piece of code is absolutely needed otherwise I pick up a whole new error of I cannot write to file. But I understand and appreciate the tip Apr 3, 2013 at 5:38

3 Answers 3

2

This should solve your problem:

parser.get('money_value','Amount',True)

of course, you could also use:

PlrMny = float(PlrMny) -.5 
parser.set('money_value','Amount',str(PlrMny))

The problem is that parser.get is expecting a string value but is reading a float. So the two options you have is to save the value as a string (that is what the 2nd option is doing), or to read the value using raw=True (that is what the 1st option is doing).

1
  • Omg thank you so much. As you cant tell I am a totally inept at all this. I have taken a programming class for the last semester and I am just trying to learn more. Thanks you so much. If I could +1 on the answer I would. Thanks man :DDDD Apr 3, 2013 at 5:57
0

I have no idea what the problem might be in line 19. How does your config looks like and did you try

 parser.getfloat('money_value','Amount')

?

1
  • Just updated it to .getfloat and same errror and my ini can be seen above in the comments on my original post Apr 3, 2013 at 5:28
0

Ok, you create a parser in one function

def rerun():
    import ConfigParser
    from ConfigParser import ConfigParser

    parser = ConfigParser()
    parser.read('Game.ini')

and you'll use it in another.

def gamble():
    PlrMny = parser.get('money_value', 'Amount')

this ought to fail since parser is a local variable in rerun and not declared in gamble.

2
  • ahh that makes sense. Let me input that and see what I get. Well it does this weird thing where it deletes the contents of the file. This giving me a "no section" error, because it is empty Apr 3, 2013 at 5:48
  • @mkind, if that were the case the error would be NameError: name 'parser' not defined. I'm guessing we are probably not seeing the entire file, and parser may in fact be a global variable.
    – amccormack
    Apr 3, 2013 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.