所以我试着通过这样做来学习,现在发生了这个错误:
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'
对于这段代码:
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)
我应该如何减去变量的输入?
请帮忙! :d
答案 0 :(得分:2)
您不能从整数中减去字符串。使用int
:
newbal = bal - int(howmuch)
答案 1 :(得分:2)
将字符串中的howmuch
输入转换为整数:
howmuch = int(input("How much?: "))
完整代码:
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)