如何在最后循环python脚本



所以我有以下测试脚本。

import time
year = time.strftime('%Y')
name = input('What is your name? ')
dob = input('What is your DOB ')
age = int(year) - int(dob)
print(f'{name}, You are {age} years old.')

打印出最后一条语句后,如何重新启动此脚本?对不起,这么愚蠢的问题,我一直在到处寻找,但什么也找不到。提前谢谢。

如果你知道要运行这段代码多少次,请使用for循环;如果它主要取决于条件,请使用while循环。要永久运行代码,请执行while True:

我会使用for语句,这也将允许您指定要运行它的次数。

import time
n = 2
for _ in range(n):
year = time.strftime('%Y')
name = input('What is your name? ')
dob = input('What is your DOB ')
age = int(year) - int(dob)
print(f'{name}, You are {age} years old.')

如果你想永远运行,那就使用while循环。

import time
while True:
year = time.strftime('%Y')
name = input('What is your name? ')
dob = input('What is your DOB ')
age = int(year) - int(dob)
print(f'{name}, You are {age} years old.')

最新更新