简体   繁体   English

在不同的行中打印字符串

[英]Print string in different lines

I almost have it! 我差不多了! This code ask your for your name, age and ask if you want more copies. 此代码询问您的姓名,年龄,并询问是否需要更多副本。 If "yes", it ask how many. 如果是,则询问多少。 I'm looking that the code prints the string in different lines, not a single one. 我正在寻找代码将字符串打印在不同的行中,而不是一行。 I tried to use a loop but didn't figure out how to structure it. 我试图使用一个循环,但没有弄清楚如何构造它。 Thanks for your time. 谢谢你的时间。

def mynameandage():

    name = (input(" Hey, What's your name?"))
    age = int(input(" and how old are you?"))
    hundred = ((100-age)+2019)
    copias1=(input("Would you like more copies of this info?"))
    displaytext= (f'{name} you will be 100 years old in the year {hundred}.   ')
    if copias1 =='yes':
        cuantas= int(input("how many?"))
        return (f"  {displaytext} " *cuantas)
    else:
        return (f'Okay but remember that {displaytext}')

mynameandage()

I think you just need to add \\n to the end of {displaytext} : 我认为您只需要在\\n {displaytext}的末尾添加\\n即可:

def mynameandage():

    name = (input(" Hey, What's your name?"))
    age = int(input(" and how old are you?"))
    hundred = ((100-age)+2019)
    copias1=(input("Would you like more copies of this info?"))
    displaytext= (f'{name} you will be 100 years old in the year {hundred}.   ')
    if copias1 =='yes':
        cuantas= int(input("how many?"))
        return (f"  {displaytext}\n" * cuantas)
    else:
        return (f'Okay but remember that {displaytext}')

mynameandage()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM