-1

I just can't figure out the problem. So i'm making a game and im using money to buy things in the game and this doesn't seem to be working. I try to write the new spent money in the .txt file and i just get an error.

with open("money.txt", "r") as rm:
game_money = rm.read()
with open("money.txt", "w") as fm:
fm.write(str(game_money))

def Function():
            ............
            slowdown_price = 20
            elif action == "buy_slowdown":
            if game_money >= 20:
                time.sleep(0.5)
                game_money -= slowdown_price
                slowdown_powerup += 1
                with open("money.txt", "w") as wm:
                    wm.write(str(game_money))

I get the following error:

TypeError: unsupported operand type(s) for -=: 'str' and 'int'
  • what error are you getting? Can you correct your indentation errors? – depperm Dec 28 '16 at 13:39
  • i tried everything converting integers to strings.. simply doesn't work. –  Dec 28 '16 at 13:41
  • always add in question **FULL** error message - there are other usefull information - ie. which line makes problem, etc. – furas Dec 28 '16 at 13:42
  • 1
    what is `slowdown_price`? `game_money` looks like it's a string. you still have indentation errors – depperm Dec 28 '16 at 13:42
  • ForceBru I tried every thing........ please help. i'm scratching my brain for a few days here –  Dec 28 '16 at 13:43

3 Answers3

1

You are using Python 2, which will happily compare integers with strings, so if game_money >= 20: works fine. However, you can't subtract an integer from a string, so game_money -= slowdown_price fails. Convert that value to an integer (or float) after you read it in:

game_money = int(rm.read())

or

game_money = float(rm.read())

If you use floating-point numbers, keep in mind that they aren't exact, so exact comparisons with == and != aren't reliable.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • I'm not using python 2 im using python 3.4 but this seems to work just fine thanks. –  Dec 28 '16 at 13:47
  • @StrozeR - No you're not. If you were using Python 3, there would be an error when you tried to see if a string was more than an integer. – TigerhawkT3 Dec 28 '16 at 13:52
  • Im positive that i use Python 3.4 compiler –  Dec 28 '16 at 13:53
  • @StrozeR - I'm sorry, but that isn't possible, given the traceback. Try adding `print(__import__('sys').version)` somewhere in your code, and check the output. – TigerhawkT3 Dec 28 '16 at 23:04
  • I get the output : 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)] –  Dec 29 '16 at 06:29
  • @StrozeR - And are you doing that in a script, in the exact same way that you are running the current script? There is literally no way for Python 3 to be successfully doing `if game_money >= 20:` with `game_money` being a string, and then failing on `game_money -= slowdown_price` two lines later. – TigerhawkT3 Dec 29 '16 at 06:32
  • @StrozeR - Do you have Python 2 installed? – TigerhawkT3 Dec 29 '16 at 06:33
  • @StrozeR - Look, there's no way for your posted code to be successfully processing that line with Python 3. It is a failing error. – TigerhawkT3 Dec 29 '16 at 06:35
  • Try it for yourself. –  Dec 29 '16 at 06:36
  • @StrozeR - I have. See [here](http://stackoverflow.com/questions/3270680/how-does-python-compare-string-and-int). Only Python 2 will process that line without a problem. – TigerhawkT3 Dec 29 '16 at 06:37
0

Reading from a file results in a string. That means that game_money is a string that contains the characters of a number, e.g "30". You should cast the string to an integer:

game_money = int(rm.read())

Do take note that this could fail if the file does not contain just a number, but other non-numerals.

0

It seams that you try to substract number slowdown_price from text game_money. You have to convert game_money to number first. ie.

game_money = int(game_money)
furas
  • 134,197
  • 12
  • 106
  • 148