0

Code with json dumps:

def intro ():
RUB_BANK = connection.call('GET', '/buy-bitcoins-online/RUB/transfers-with-specific-bank/.json').json()
    amount2 = json.dumps(RUB_BANK['data']['ad_list'][0]['data']['temp_price'])
    print(amount2)
intro()

This code work. It gives me correct result:

"916000.00"
But if i try:
def intro ():
RUB_BANK = connection.call('GET', '/buy-bitcoins-online/RUB/transfers-with-specific-bank/.json').json()
    amount2 = json.dumps(RUB_BANK['data']['ad_list'][0]['data']['temp_price'])
    print(amount2 + 20000)
intro()

Give me traceback

TypeError: can only concatenate str (not "int") to str

I need to plus 20000 to 916000.00 How to do it?

Antony
  • 1
  • 2
  • 1
    It's because amount2 is a string, not a number, so you're doing `"916000.00" + 20000`. What you need to do is convert/cast that string into a number. –  Oct 15 '20 at 17:25

1 Answers1

0

The problem is that amount2 is a string at that point.

You simply need to do this:

amount2 = RUB_BANK['data']['ad_list'][0]['data']['temp_price']
print(float(amount2) + 20)
Colin
  • 4,025
  • 21
  • 40
  • invalid literal for int() with base 10: '"915000.00"' – Antony Oct 15 '20 at 17:27
  • Note the guidance in [How to Answer](https://stackoverflow.com/help/how-to-answer) about questions that have been "asked and answered many times before". – Charles Duffy Oct 15 '20 at 17:29
  • @Antony - I fixed it. Use float() instead of int() – Colin Oct 15 '20 at 17:29
  • could not convert string to float: '"911000.00"' – Antony Oct 15 '20 at 17:30
  • @Antony, using `json.dumps()` is what's adding the literal quotes to your string. Don't do that -- you want to add just `float(RUB_BANK['data']['ad_list'][0]['data']['temp_price'])`, not `float(json.dumps(RUB_BANK['data']['ad_list'][0]['data']['temp_price']))` – Charles Duffy Oct 15 '20 at 17:30
  • I tried it many time. And look at another questions. – Antony Oct 15 '20 at 17:31
  • @Antony, ...the reason the other questions' answers don't help you is because you'se using the extra `json.dumps()`, so your strings have quotation marks in them, which `int()` or `float()` can't handle. Just take out that function call -- you have no reason to have it -- and the problem goes away. – Charles Duffy Oct 15 '20 at 17:32
  • @Antony - Fixed it by stripping out the quotes. – Colin Oct 15 '20 at 17:35
  • @Colin, ...why would you advise removing the quotes after-the-fact, as opposed to using code that never adds quotes in the first place? – Charles Duffy Oct 15 '20 at 17:36
  • @CharlesDuffy - Your solution I better. I quickly offered mine in crosstalk, but will adjust my answer. – Colin Oct 15 '20 at 17:52