簡體   English   中英

在不同的行上打印多次 Python

[英]Print multiple time on different lines 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)

誰能幫我解決這個問題?
謝謝!

如果你想使用你的代碼,只需添加'\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}. ")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM