为了账号安全,请及时绑定邮箱和手机立即绑定

对包含变量的段落进行文本换行

对包含变量的段落进行文本换行

慕后森 2023-07-18 15:17:38
所以我有以下代码:animal = str(input("A prey animal (singular): "))habitat = str(input("A habitat, such as savannah or rainforest: "))action_verb = str(input("A motion related action verb (ending with ing) such as moving or running: "))onomatopeia = str(input("A sound an animal makes, such as growl or bark: "))number_1 = int(input("An integer between 2 and 9: "))predators = str(input("A predator that eats your prey animal (plural): "))insects = str(input("A terrible swarming insect that lives in your chosen habitat (plural): "))number_2 = number_1**number_1*100print("\tAlong came a", animal, action_verb, "through the", habitat + ". All of a sudden,"     "\nthere was a loud", onomatopeia, "and the", animal, "looked around for a good place to"     "\nhide. There was a pack of", number_1, predators, "coming right towards the poor", animal+     ". However, \nall of the", predators, "had to run, because", number_2, insects, "were"    "swarming the\n", predators, "and they had to hightail it out "    "of there. \n\"Phew, that's a relief,\" thought the", animal + ", \"man, I really could"     "have \nbeen toast if not for those", insects + ".\"")我真的不知道在这里要做什么,因为由于变量的原因,我无法将其输出到每行设置为 80 个字符的段落中。我知道有一个 textwrap 模块,但它只适用于我尝试过的字符串。有谁知道我如何才能在输出时很好地格式化它?
查看完整描述

3 回答

?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

您可以使用 textwrap,但首先将其格式化为:"This variable {} is in the sentence".format(variable)



查看完整回答
反对 回复 2023-07-18
?
芜湖不芜

TA贡献1796条经验 获得超7个赞

这是另一个格式化系统。

print(f"{current_variable} said hello!")

当 current_variable 是James它的格式字符串时

"James said hello!"


查看完整回答
反对 回复 2023-07-18
?
慕村225694

TA贡献1880条经验 获得超4个赞

您可以使用更新的 f 字符串:

print(f"\t有一个,{动物},{action_verb},穿过,{栖息地}。突然间,.....等等"


查看完整回答
反对 回复 2023-07-18
  • 3 回答
  • 0 关注
  • 80 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信