我在尝试解决此代码时遇到了麻烦,但我似乎无法弄清楚问题出在哪里,有人可以帮忙
def main():
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Welcome to the Movie Store!")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
budget = int(input("What is your budget?"))
print("Your budget is " + budget + "$.")
sale = (200 - budget)
if(budget < 200):
print("print("If you spend $" + sale + "you'll qualify for a gift!")
else:
print("You qualify for a free gift!")
main()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-163-65823cbb31f3> in <module>
3 print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
4 budget = int(input("What is your budget?"))
----> 5 print("Your budget is " + budget + "$.")
6 sale = (200 - budget)
7
TypeError: can only concatenate str (not "int") to str
答案 0 :(得分:1)
在budget
中用str(budget)
替换print("Your budget is " + budget + "$.")
。
答案 1 :(得分:1)
错误提示:
TypeError:只能将str(而不是“ int”)连接到str
您不能将字符串和if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
var sumedges = window.innerWidth + window.innerHeight;
window.onresize = function () {
if (window.innerWidth + window.innerHeight < sumedges) {
const footerArr = document.getElementsByClassName("footer");
for (let i = 0; i < footerArr.length; i++) {
footerArr[i].style.bottom = "auto";
}
} else {
const footerArr = document.getElementsByClassName("footer");
for (let i = 0; i < footerArr.length; i++) {
footerArr[i].style.bottom = "0";
}
}
};
}
(整数)结合在一起。
更改:
budget
收件人:
print("Your budget is " + budget + "$.")
答案 2 :(得分:1)
def main():
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Welcome to the Movie Store!")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
budget = int(input("What is your budget?"))
print("Your budget is " + budget + "$.")
sale = (200 - budget)
if(budget < 200):
print("print("If you spend $" + sale + "you'll qualify for a gift!")
else:
print("You qualify for a free gift!")
main()
您收到此错误,因为您没有在打印前将预算从int转换为string。 因为我们只能将字符串连接为字符串。
它们有很多方式,例如:
1》在打印前将预算转换为str:
print("Your budget is " + str(budget) + "$.")
2》在str中使用格式:
print("Your budget is {} $" .format(budget))
3》使用f字符串:
print(f"Your budget is {budget} $.")
您已在这里使用print 2次,并且在这里您还必须使用上述任何一种方式在打印之前将sale转换为string:
if(budget < 200):
print("print("If you spend $" + sale + "you'll qualify for a gift!")
else:
print("You qualify for a free gift!")
希望您发现此解决方案有用... !!
答案 3 :(得分:0)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Welcome to the Movie Store!")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
budget = input("What is your budget?")
print("Your budget is " + budget + "$.")
budget = int(budget)
sale = (200 - budget)
if(budget < 200):
print("If you spend")
else:
print("You qualify for a free gift!")
您不需要立即将预算切换到变量,因为您将在输入后立即打印预算(您不能打印带有整数的字符串)。所以我将其更改为在程序需要它之前立即将其转换为整数。希望这对您有帮助!
答案 4 :(得分:0)
连接的替代方法是使用f字符串。我发现文档中的these examples非常有用。
print(f"Your budget is {budget}$.")
此处,budget
变量可以是str
,int
类型,也可以是可以转换为字符串的任何其他数据类型。
使用f字符串的好处是,您完全不必担心类型转换。 Python将为您完成这些任务。您甚至还可以使用从函数调用返回的结果。
# Sample code
print(f"This length of this list is {len(lst)}.")
答案 5 :(得分:0)
使用此:
print("Your budget is ",budget,"$.")
答案 6 :(得分:0)
该错误消息提供了一些有用的信息。它甚至可以告诉您错误发生的确切位置。在这种情况下,它告诉您TypeError
上有一个line 5
:
Traceback (most recent call last):
File "C:\Users\Chris\Desktop\deleteme\deleteme.py", line 5, in <module>
print("Your budget is " + budget + "$.")
TypeError: must be str, not int
如果滚动到第5行,您会看到它是:
print("Your budget is " + budget + "$.")
因此,问题在于您试图添加类型str + int + str
。那不行您必须将int转换为字符串。可以将其视为str + str(int) + str
,或在您的代码中:
print("Your budget is " + str(budget) + "$.")
或者,我要做的方法是使用'f-strings'。您只需要在第一个引号前加上f
,然后将变量放入大括号({}
)中。这样,您甚至不需要使用str()
:
print(f"Your budget is {budget} $.")
答案 7 :(得分:0)
您可以在python中使用格式化的字符串: 您可以使用strng + int + strng这不是在python中打印的方法,您可以通过格式化的字符串进行打印,例如: print(f“您的预算为{budget} $。”)