在不同行上打印多个时间

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在不同行上打印多个时间相关的知识,希望对你有一定的参考价值。

我写了一段代码,我想在不同的行上打印多个时间,我写了下面的代码,我的问题是关于最后一行代码。

import math 

# Make an program, ask the user how old he or she is and tell them the year
# they turn 100

name = input("What is your name? ")
age = input("How old are you? ")
random_number = input("Please give a random number between one and ten: ")
age = int(age)
random_number = int(random_number)

year_awnser = 100 - age

year_awnser = int(year_awnser)

print(f"It will take {year_awnser} years until you are 100")


awnser = 2020 + year_awnser

awnser = int(awnser)

print(f"{name} you will be 100 in: {awnser}")

print("\n")

# Print the message above, times given by the random_number.

print(f"{name} you will be 100 in: {awnser}. " * random_number)

谁能帮我解决这个问题? 谢谢你的帮助

答案

如果你想使用你的代码,只需在代码中加入 '\n' 标签。

print(f"{name} you will be 100 in: {awnser}. \n" * random_number)

我推荐使用循环,因为它能给人更好的印象,并使代码更清晰。打印方法有一个内置的 '\n' 标签中。

for times in range(random_number):
    print(f"{name} you will be 100 in: {awnser}. ")

以上是关于在不同行上打印多个时间的主要内容,如果未能解决你的问题,请参考以下文章