问答详情
源自:4-2 Python之if-else语句

为啥这个代码运行后输出的是adult

age=('17')

if age<18:

  print('teenager')

else:

      print('adult')


提问者:weixin_慕容6030059 2023-06-13 23:15

个回答

  • 精慕门9457232
    2024-01-17 21:17:46

    你的17定义的是字符串......把引号去了就行

  • 慕勒0532217
    2023-09-10 11:32:07

    age=('17')你这样给是字符串,正常会产生错误,如下not supported between instances of 'str' and 'int',

    改成age =17即可

  • weixin_慕工程7111902
    2023-06-14 17:32:24

    Computer regarded your

    『('17')』 of   『age=('17')』 as a 'string',not a 'number'.

    So it's working like down here

    else:

          print('adult')


    If you change your code from

    ____________________________________

    age=('17')      <-----------------------

    if age<18:

      print('teenager')

    else:

          print('adult')

    ____________________________________


    to


    ____________________________________

    age=17       


      <------------------------

    if age<18:

        print('teenager')

    else:

        print('adult')

    ____________________________________



    That will  be correct.

    Enjoy your coding.(^_^)