我正在制作一个基于文本的游戏并且一直很好但现在但我遇到了int
的错误。到目前为止我的代码看起来像这样:
money = 500
print("You Have $500 To Spend On Your City. The Population Is 0 People")
input_var3 = input("What Will You Spend Your Money On? A House Or A Restraunt. ")
if input_var3 == "House":
money - 100
print("You Have Spent Your Money On A House")
print("You Now Have $" + money)
if input_var3 == "Restraunt":
money - 150
print("You Have Spent Your Money On A Restraunt")
print("You Now Have $" + money)
你的钱等于500美元,但是如果你把它花在一个房子或限制上,你将会有更少的钱,而且外壳会打印你剩下多少钱。但是我总是得到这个错误:
Traceback (most recent call last):
File "C:\Users\Jason\Desktop\City Text.py", line 11, in <module>
print("You Now Have $" + money)
TypeError: Can't convert 'int' object to str implicitly
我已经意识到我必须做一个sting而不是Int,但我不知道该怎么做。有人可以帮忙吗?
答案 0 :(得分:3)
money
变量是整数。将它们连接在一起时,不能将整数与字符串混合在一起。使用str()
函数将整数转换为字符串:
print("You Now Have $" + str(money))
另外,我认为你打算从货币价值中拿走100。 money - 100
只返回500 - 100
,即400
。如果您想让money
等于400
,请执行:
money -= 100
相当于:
money = money - 100
答案 1 :(得分:1)
您需要将值存储在变量中。
if input_var3 == "House":
money -= 100 # Notice the usage of -=
print("You Have Spent Your Money On A House")
print("You Now Have $" + str(money)) # Type casting
if input_var3 == "Restraunt":
money = money - 150 # Same as -=
print("You Have Spent Your Money On A Restraunt")
print("You Now Have $" + str(money)) # Type casting
答案 2 :(得分:0)
使用:
print("You Now Have $" + str(money))
您无法将字符串与数字连接,因此请将您的数字转换为字符串。
答案 3 :(得分:0)
您可以使用str()
功能。例如:
print("You Now Have $" + str(money))