我在修复此代码时遇到问题,有人可以帮我吗,我不知道如何解决“销售”功能的帮助,谢谢您
def main():
#store introduction
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Welcome to the Movie Store!")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
#user input on budget
budget = (input("What is your budget?"))
print("Your budget is " + budget + "$.")
# declaring variables
sale = (200 - int(budget))
# selection statements to determine subtotal
if (200 > int(budget)):
print("If you spend $" + sale + " you'll qualify for a gift!")
else:
print("You qualify for a free gift!")
print("We recommend our all time best seller Marvels Black Panther!")
main()
答案 0 :(得分:6)
使用f字符串进行变量替换(还有许多其他方式):
print(f"Your budget is {budget}$.")
print(f"If you spend ${sale} you'll qualify for a gift!")
当前,在您的代码中,您尝试将字符串“ Your budget is”添加到整数budget
,而python不知道该怎么做。另一种方法是将变量显式转换为字符串:
print("Your budget is " + str(budget) + "$.")
或使用格式:
print("Your budget is %s$." % (budget,))
答案 1 :(得分:1)
您的变量sale是一个整数,在您的打印语句生效之前,必须将其转换为字符串。
执行此操作
if (200 > int(budget)):
print("If you spend $" + str(sale) + " you'll qualify for a gift!")
else:
print("You qualify for a free gift!")
str()函数将sale变量转换为字符串,以便可以与+运算符结合并打印到控制台。