【问题标题】:Unsupported Operand Type with ConfigParserConfigParser 不支持的操作数类型
【发布时间】:2013-04-03 04:26:36
【问题描述】:

我正在尝试制作一个游戏,玩家每次玩游戏时都会从 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 PlrMny

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():

    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
        PlrMny == type(int)
        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',PlrMny - .5)
                parser.write (configFile)
                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 181, in <module>
    rerun()
  File "C:\Python27\Gamb Game.py", line 180, in rerun
    gamble()
  File "C:\Python27\Gamb Game.py", line 174, in gamble
    input()
  File "C:\Python27\Gamb Game.py", line 42, in input
    parser.set('money_value','Amount',PlrMny - .5)
    TypeError: unsupported operand type(s) for -: 'str' and 'float'

帮助?

【问题讨论】:

  • 以后,请务必使用python标签标记python问题。关注python标签的人会首先看到python标签的问题。

标签: python parsing types configparser


【解决方案1】:

您试图从字符串中减去 0.5,而不是浮点数。也就是说,您的变量 PlrMny 是一个字符串。

换行

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

parser.set('money_value','Amount',str(float(PlrMny) - .5)) #if you want money_value to be a string

但是,使用parser.set 不会改变正在运行的程序中PlrMny 的值。它只会更改 .ini 文件上下文中的值。所以,你可能想要做的是使用这个代码:

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

您需要在写入 ini 时将 PlrMny 转换为字符串,以避免读取 ini 文件时出错。

【讨论】:

  • 好吧,我不敢相信我以前没有这样做...谢谢。但是,当我这样做时,值保持在(ini 文件中的 8.5 但由于某种原因四舍五入,可能是因为它是浮点数?) 9. 此外,ini 文件看起来像:(使用 3 次后)
  • [money_value] 金额 = 8.5 [money_value] 金额 = 8.5 [money_value] 金额 = 8.5
  • 它不应该打印 3 次,而是编辑第一个,它也应该减少 0.5。帮忙?
  • @user2238780,我认为这是因为您从不关闭文件。由于在循环中文件保持打开状态,因此您将附加到现有文件。
  • 好吧,你是对的。但是现在,当我运行程序时,它会更新为正确的值,但是,在运行时,它会打印与我开始时相同的数字,但是当我关闭程序时会更新
猜你喜欢
  • 2021-08-17
  • 2017-07-15
  • 2020-02-29
  • 2011-03-08
  • 2016-11-28
  • 2020-12-04
  • 1970-01-01
相关资源
最近更新 更多