【问题标题】:Should I put every time the a=int(input())...?我应该每次都输入 a=int(input())... 吗?
【发布时间】: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'))

【问题讨论】:

标签: python python-3.x loops input while-loop


【解决方案1】:

我认为这应该更好

presentYear = int(2020)
while True:
    a = int(input('enter your birth year\n'))
    if (a>1900 and a<2020):
        age = presentYear-a
        print("Your age is "+ str(age) )
    else:
        print('error')

【讨论】:

    猜你喜欢
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 2011-08-25
    相关资源
    最近更新 更多