python - 在不同的行上多次打印 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}. ")
推荐阅读
- java - 如何在 AppLayout 中将 Component ranged right 添加到 Navbar?
- python-3.x - 检查子字符串的最佳方法
- isabelle - 是否可以使用 Isabelle 中的策略“释放”顶级通用量化变量?
- apache-nifi - 如何测试 NiFi 自定义处理器进行 Rest 调用(HTTPPost)并检查响应状态?
- python - 从数据框中按十年堆叠直方图
- java - 每当Java中出现关键字时,如何在字符串中引入换行符?
- python - Python Keras 运行错误(无效语法)
- python - 有没有另一种方法可以在没有 matplotlib 的情况下在 python 中绘制图形?
- charts - 条形图背景操作
- python - 标准化 4D 数组的最有效方法是什么?