【发布时间】:2020-07-24 22:47:12
【问题描述】:
有什么更好的方法可以减少我的代码吗?
a=int(input('birth year'))
present_year=2020
while a<1900 or a>2020:
print('U are funny, arent you ? ')
a=int(input('ENTER AGAIN PLEASE, your birth year: '))
while a>=1900 and a<2020:
print('you are ',(present_year-a),'years old')
a=int(input('NEXT ONE : '))
while a<1900 or a>2020:
print('Error ,')
a=int(input('RE-ENTER YOUR BIRTH YEAR'))
【问题讨论】:
-
是的。您可以查找https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response 以获得一些灵感。然后清理您的代码,使用函数进行输入验证并阅读
while True: ..... break语法以转义循环。教授编程并不是 SO 的强项,所以也许可以做一些教程。
标签: python python-3.x loops input while-loop