Python基础知识(功能) [英] Python basics (functions)

查看:103
本文介绍了Python基础知识(功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用python教程进行编程,但是我陷入了这个问题:



任务:


  1. 使用一个参数定义一个函数distance_from_zero。
  2. 具有该函数:


    1. 检查它接收的输入的类型。

    2. 如果类型是int或float,则函数应该返回函数输入的绝对值。 $ b
    3. 如果该类型是任何其他类型,则函数应返回不是整数或浮点数!。

    我的回答(不起作用):

      def distance_from_zero(d ):
    如果输入(d)== int或float:
    返回abs(d)
    else:
    返回不是整数或浮点数!

    我不知道我做错了什么...谢谢

    解决方案

    你不能使用这种基于自然语言的逻辑连接。

     如果输入(d)== int或者type(d)== float 

    这样你就可以进行两种比较了, (d)== int 以及 type(d)== float 。其结果可以与 -operator结合使用。


    i am starting programming with a python tutorial, but i am stuck on this problem, :

    Task:

    1. define a function, "distance_from_zero", with one parameter.
    2. have that function do the following:

      1. Check the type of the input it receives.
      2. If the type is int or float, the function should return the absolute value of the function input.
      3. If the type is any other type, the function should return "Not an integer or float!"

    My answer (that does not work...):

    def distance_from_zero(d):
        if type(d) == int or float:
            return abs(d)
        else:
            return "Not an integer or float!"
    

    i don´t know what i am doing wrong... Thank you

    解决方案

    You cannot use this kind of "natural language based logic concatenation". What I mean is that you need to state the parts of your logical conditions explicitly.

    if type(d) == int or type(d) == float
    

    This way you have the two comparisons, which stand for themselves: if type(d) == int as well as type(d) == float. The results of this can be combined with the or-operator.

    这篇关于Python基础知识(功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆