Python 如何从现有变量中减去输入?
因此,我试图通过实践学习,现在出现了以下错误:Python 如何从现有变量中减去输入?,python,Python,因此,我试图通过实践学习,现在出现了以下错误: 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' 我应该如何从变量中减去输入 请
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'
我应该如何从变量中减去输入
请帮忙D不能从整数中减去字符串。使用:
将字符串中的
输入量转换为整数:
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)
将其转换为int
:)谢谢!差点忘了:D如果你的问题解决了,请接受答案。
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)