【问题标题】:Creating a line break while outputting a string multiple times in python在python中多次输出字符串时创建换行符
【发布时间】:2021-11-30 16:04:32
【问题描述】:

这里是代码sn-p。

我试图让用户输入他们的姓名和年龄,以输出他们将在哪一年 100 岁。

然后我向用户询问一个数字,并希望将字符串重复 x 次。

当我尝试使用换行符 '\n' 时,我收到了意外的字符错误。不确定我应该去哪里。

name = input ("What is your name: ")
age = input("what is your age: ")
age = int(age)
howLong = (100 - age) + 2021 #calculates the year
times = input("Pick a number: ")# asks user for a year
print(int(times) * (name+" will be 100 years old in: " + str(howLong))) #tells what year it will be

我试过了

print(int(times) * ('name+" will be 100 years old in: " + str(howLong))\n') #tells what year it will be

还有

print(int(times) * (name+" will be 100 years old in: " + str(howLong))'\n') #tells what year it will be

还有

print(int(times) * (name+" will be 100 years old in: " + str(howLong))\n) #tells what year it will be

还有

print(int(times) * ('''name+" will be 100 years old in: " + str(howLong))'''\n) #tells what year it will be

它似乎不起作用。任何指针?是的,这是作业,这部分是奖金。试图弄清楚。

【问题讨论】:

    标签: python string output repeat


    【解决方案1】:

    换行符需要在字符串文字中,然后可以附加到字符串的其余部分。

    print(int(times) * (name + " will be 100 years old in: " + str(howLong) + '\n'))
    

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 2021-11-24
      • 1970-01-01
      • 2011-04-21
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 2022-01-08
      相关资源
      最近更新 更多