首页 > 解决方案 > 在不同的行上多次打印 Python

问题描述

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

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)

谁能帮我解决这个问题?
谢谢!

标签: pythonprinting

解决方案


如果你想使用你的代码,只需添加'\n'标签:

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

我建议使用循环,因为它给人更好的印象并澄清了代码。print 方法中有一个内置'\n'标签:

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

推荐阅读