简体   繁体   English

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

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

Can someone tell me what is wrong with this python code?有人能告诉我这个 python 代码有什么问题吗? I am a newbie and have been trying to run this:我是新手,一直在尝试运行它:

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')

the function needs to be called.需要调用 function。 try this:尝试这个:

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

Another way is to have the input statement inside the function, which makes it cleaner:另一种方法是将 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: Output:

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

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

相关问题 有人能告诉我我的代码有什么问题吗 - Can someone tell me what is wrong with my code 有人可以告诉我我的 animation 代码有什么问题吗? - Can someone tell me what's wrong with my animation code? python 中的变量不起作用,有人可以告诉我我的代码有什么问题吗? - Variable in python not working, can someone tell me what is wrong with my code? 有人能告诉我这段代码有什么错误吗? - Can someone tell me what are the mistakes in this code? 有人可以告诉我我的代码有什么问题吗? 我在调试时遇到问题 - Can someone tell me what is wrong with my code? I am having problems debugging 有人可以告诉我我做错了什么[暂停] - Can someone tell me what I'm doing wrong [on hold] 有人可以告诉我我的WIP Python hangman游戏有什么问题吗? - Can someone tell me what is wrong with my WIP Python hangman game? 如何将图像数据正确拟合到 python 中的 model? 有人可以告诉我我做错了什么吗? - How to fit image data correctly to a model in python? Can someone tell me what i did wrong? 有人可以告诉我我做错了什么新到 Python - Can someone please tell me what am I doing wrong New to Python 有人可以告诉我我做错了什么吗? - Can someone tell me what am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM