-2

So im trying to learn by doing and now this error occured:

Traceback (most recent call last):
  File "C:/Users/Marcel/PycharmProjects/untitled/bank.py", line 14, in <module>
    newbal = bal - howmuch
TypeError: unsupported operand type(s) for -: 'int' and 'str'

For this Block of code:

id = input("Enter Bank ID: ")
bal = 1500

if id == "12345":
    print ("Correct!")
    print("You have a total Balance of",bal,"$")
    choi = input("What do you want to do? (Q)uit or (T)ake Money: ")

    if choi == "Q":
        quit()
    if choi == "T":
        howmuch = input("How much?: ")
        newbal = bal - howmuch
        print ("You took",howmuch,"$ from your Bank Account!")
        print ("Total Balance:",newbal)

How am i supposed to substract the input from the variable?

Please help! :D

Venx
  • 87
  • 1
  • 10

2 Answers2

2

You can't substract a string from an integer. Use int:

newbal = bal - int(howmuch)
2

Convert your howmuch input from a string into an integer:

howmuch = int(input("How much?: "))

In full code:

id = input("Enter Bank ID: ")
bal = 1500

if id == "12345":
    print ("Correct!")
    print("You have a total Balance of",bal,"$")
    choi = input("What do you want to do? (Q)uit or (T)ake Money: ")

    if choi == "Q":
        quit()
    if choi == "T":
        howmuch = int(input("How much?: "))
        newbal = bal - howmuch
        print ("You took",howmuch,"$ from your Bank Account!")
        print ("Total Balance:",newbal)
Mark Skelton
  • 3,663
  • 4
  • 27
  • 47