繁体   English   中英

有人能告诉我这个 python 代码有什么问题吗?

[英]Can someone tell me what is wrong with this python code?

有人能告诉我这个 python 代码有什么问题吗? 我是新手,一直在尝试运行它:

legs = input('Number of legs he have?\n')
def Dance(legs):
    if legs = 'Two':
        print('HE HAVE TWO LEGS SO HE CAN  DANCE')
    elif legs:
        print('HE HAVE ONE LEG AND HE CAN DO SOMETHING')
    else:
        print('NO LEGS SO HE CANT DANCE')

需要调用 function。 尝试这个:

legs = int(input('Number of legs he have?\n'))
def Dance(legs):
    if legs == 2:
        print('HE HAVE TWO LEGS SO HE CAN  DANCE')
    elif legs == 1:
        print('HE HAVE ONE LEG AND HE CAN DO SOMETHING')
    else:
        print('NO LEGS SO HE CANT DANCE')

Dance(legs) # this invokes/calls the function

另一种方法是将 input 语句放在 function 中,这样更简洁:

def Dance():
    legs = input('Number of legs he has?\n')
    if legs == 'Two':
        print('HE HAS TWO LEGS SO HE CAN  DANCE')
    elif legs == 'One':
        print('HE HAS ONE LEG AND HE CAN DO SOMETHING')
    elif legs == 'Zero':
        print('NO LEGS SO HE CANNOT DANCE')
    else:
        print('I do not understand your input')

Dance()   #this invokes/calls the function

Output:

Number of legs he has?
 One
HE HAS ONE LEG AND HE CAN DO SOMETHING

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM