【问题标题】:Iterable Floats?可迭代浮点数?
【发布时间】:2013-04-03 05:16:15
【问题描述】:

我正在尝试制作一个游戏,玩家每次玩游戏时都会从 ini 文件中的值中扣除 0.5。但是我不断收到错误,我不知道该怎么做。这是我的代码。不要担心 cmets,那些是给我的,我稍后会关闭 while 循环。这只是代码的一部分。顺便说一句,代码有效,只是不是这个。谢谢。

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

【问题讨论】:

  • 你的ini文件是什么样的?
  • [money_value] 金额 = 9.0 (在两条不同的行上)
  • @user2238780 如果您花时间清理您的问题,您将获得更好的帮助。您需要删除不需要的代码(即configFile = open("C:\Python27\Game.ini", "w")),删除与问题本身无关的任何代码通常很有帮助。这个过程需要时间,但这是一个非常好的调试策略,当需要阅读的代码较少时,人们更愿意提供帮助
  • 我不太确定,但也许解析器区分大小写?
  • @amccormack 实际上,那段代码是绝对需要的,否则我会发现一个全新的错误,即我无法写入文件。但我理解并感谢提示

标签: python parsing numbers iterable configparser


【解决方案1】:

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

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

当然,你也可以使用:

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

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

【讨论】:

  • 天哪,非常感谢。正如你不能说的那样,我在这一切方面完全无能。我上个学期上了编程课,我只是想了解更多。非常感谢。如果我可以对答案+1,我会的。谢谢大佬:DDDD
【解决方案2】:

我不知道第 19 行的问题可能是什么。您的配置看起来如何,您是否尝试过

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

?

【讨论】:

  • 刚刚将其更新为 .getfloat 和相同的错误,我的 ini 可以在我原始帖子的 cmets 上方看到
【解决方案3】:

好的,你在一个函数中创建一个解析器

def rerun():
    import ConfigParser
    from ConfigParser import ConfigParser

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

你会在另一个中使用它。

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

这应该会失败,因为解析器是重新运行的局部变量,而不是在赌博中声明。

【讨论】:

  • 啊,有道理。让我输入,看看我得到了什么。好吧,它做了一件奇怪的事情,它删除了文件的内容。这给了我一个“没有部分”的错误,因为它是空的
  • @mkind,如果是这种情况,错误将是 NameError: name 'parser' not defined。我猜我们可能看不到整个文件,解析器实际上可能是一个全局变量。
猜你喜欢
  • 2012-08-07
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 2021-04-27
  • 1970-01-01
相关资源
最近更新 更多