【发布时间】:2013-05-27 06:51:35
【问题描述】:
我正在制作一个简单的基于文本的游戏,但遇到了错误。我必须将代码中的 int 转换为 str。我的代码如下所示:
tax1 = input("You May Now Tax Your City. Will You? ")
if tax1 == "Yes" or tax1 == "yes":
tax2 = input("How Much Will You Tax Per Person In Dollars? ")
if tax2 > 3:
print("You Taxed To High! People Are Moving Out")
time.sleep(1.5)
population -= (random.randint(2, 4))
print("The Population Is Now " + str(population))
time.sleep(1.5)
money += (population * 2)
print("From The Rent You Now Have $" + str(money) + " In Total.")
if tax2 < 3:
print("You Have Placed A Tax That Citizens Are Fine With.")
time.sleep(1.5)
money += (tax2+(population * 2))
print("From The Rent And Tax You Now Have $" + str(money) + " In Total")
我将在我的代码中添加什么来做到这一点?
【问题讨论】:
-
请发布错误和堆栈跟踪。只是告诉我们您遇到了错误并没有帮助。
-
作为参考,您可能希望使用
if tax1.lower() == "yes"而不是if tax1 == "Yes" or tax1 == "yes"- 它更易于阅读并为用户提供更多选择(大写和小写的任意组合)。 -
你必须照顾好一个城市,这就是税收的原因!
-
@thegrinner 没有谈论为什么要征税——评论是关于澄清你的代码。是
str(money)还是str(population)的问题?你得到的实际错误是什么?您在调用str()函数时查看过population和money的值吗?