可迭代的浮点数? [英] Iterable Floats?

查看:158
本文介绍了可迭代的浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个游戏,在每次玩游戏时,都要从ini文件中的值中扣除.5.但是,我不断收到错误消息,我不确定该怎么办.这是我的代码.不必担心评论,这些评论是给我的,以后我会关闭while循环.这只是代码的一部分.顺便说一句,代码有效,但并非如此.谢谢.

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"

这是错误:

 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

推荐答案

这应该可以解决您的问题:

This should solve your problem:

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

当然,您也可以使用:

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

问题在于parser.get需要一个字符串值,但正在读取浮点数.因此,您拥有的两个选项是将值另存为字符串(这是第二个选项的作用),或使用raw = True读取值(这是第一个选项的作用).

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

这篇关于可迭代的浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆