下面我做了一个程序,问你三个不同的问题,并用一句话总结。我在下面尝试的方式给了我一个错误,当我把+ choice +choice2 + choice3所有的都放在最后时,用户输入的答案最后会堆积起来。我应该如何将三个用户输入分散在句子的特定位置?choice = input("What is your favorite food?")choice2 = input("What is your favorite color?")choice3 = input("What is your favorite car?")print("So your favorite food is " + choice "and your favorite color is " + choice2 "and your favorite car is " + choice3)我已经对这个网站Python 用户输入进行了一些研究,但仍然找不到我的问题的答案。任何帮助,将不胜感激。
4 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
更改打印语句
print("So your favorite food is " + choice + "and your favorite color is " + choice2 +"and your favorite car is " + choice3)
或更清洁的解决方案是使用 fstrings
print(f"So your favorite food is {choice} and your favorite color is {choice2} and your favorite car is {choice3}")
蝴蝶不菲
TA贡献1810条经验 获得超4个赞
您在上面发布的内容几乎是正确的,但是您错过了两个 + 运算符(在选择和选择 2 之后)。
print("So your favorite food is " + choice + "and your favorite color is " + choice2 + "and your favorite car is " + choice3)
格式化字符串的更好方法是使用字符串格式化语法。
旧式是:
print("So your favorite food is %s and your favorite color is %s and your favorite car is %s" % (choice, choice2, choice3))
用于字符串格式化的更现代的 Python 语法是:
print("So your favorite food is {c1} and your favorite color is {c2} and your favorite car is {c3}".format(c1=choice, c2=choice2, c3=choice3))
更多关于字符串格式的信息在这里
BIG阳
TA贡献1859条经验 获得超6个赞
您缺少+
操作员。将您的代码更改为print("So your favorite food is " + choice + " and your favorite color is " + choice2 + " and your favorite car is " + choice3)
当+
运算符被 2 string
s 夹住时,它会连接string
s。
守着星空守着你
TA贡献1799条经验 获得超8个赞
我更喜欢格式化字符串以获得更简洁的方法,如下所示:
print("So your favorite food is {} and your favorite color is {} and your favorite car is {}".format(choice, choice2, choice3))
添加回答
举报
0/150
提交
取消