0

When I try to run this code

budget = 500
x = 1
y = 1
bet = 0
totalsum = 0
x = input("enter result:")
bet = input("enter bet:")
y = input("you bet on?:")
if x == y:
    bet = (bet) * 2
    totalsum = budget + bet
    budget = totalsum
    print("your winnings:", totalsum)
    print("your budget:", budget)
if x != y:
    totalsum = budget - bet
    budget = totalsum
    print("your winnings:", totalsum)
    print("your budget:", budget)
if budget < 0:
    print("you lost :(")

I get a console error:

Traceback (most recent call last):

File "main.py", line 19, in
totalsum = budget + bet TypeError: unsupported operand type(s) for +: 'int' and 'str'
Program finished with exit code 1

I don't understand why. Both those values are set to have a numerical value so why is one of them a string? (I think that it what it means by 'str')

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
papajo
  • 167
  • 1
  • 8
  • 1
    ```input()``` returns ```str``` on default - you need to replace every ```input(...)``` with ```int(input(...))``` – Grzegorz Skibinski Feb 06 '20 at 18:07
  • Thanks your answer was the best because I understood as well why bet wasnt an integer while others just fed me with the answer (but thanks to those too of course) – papajo Feb 06 '20 at 18:22

3 Answers3

3

Your bet is not an integer, it is a string. Cast it to an integer with int(bet).

Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25
  • I don't think that *casting* is a thing in python. The word you are looking for is convert, which is what `int(bet)` is doing. – quamrana Feb 06 '20 at 18:18
3

Any value you get from input will always be a string, hence your error. To fix this, cast it to an int:

bet = int(input("enter bet:"))
Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
0

You are trying to add a string to an int

Do this instead:

totalsum = budget + int(bet)
CobraPi
  • 372
  • 2
  • 9