小编hol*_*sor的帖子

在if语句之后Python不返回任何内容

我需要一些指导,告诉我为什么在运行程序后没有在shell中打印任何内容.这是我的代码:

def numbersize1(x):
    """returns the string 'Negative!' if the number is less than zero, 'Small!' if the number is at least zero but less than 10, 'Medium!' if the number is at least 10 but less than 100, and 'Large!' if the number is greater than or equal to 100.

    Number -> str"""
    if x<0:
        return str('Negative!')
    if x==0 and x<10:
        return str('Small!')
    if x==10 and x<100:
        return str('Medium!')
    if x==100 and x>100:
        return str('Large!')
Run Code Online (Sandbox Code Playgroud)

正如我之前所说,当我运行脚本时,shell中没有任何内容.换句话说,如果我输入'1',字符串'Small!' 应该出现,而不是.任何帮助表示赞赏.

python python-3.x

1
推荐指数
1
解决办法
131
查看次数

用str,float,int和元组划分

基本上,我希望能够将int,str,tuple和float分成两半.我在区分输入类型然后返回相同类型时遇到问题.

例如,一半(7)应该返回3(不是3.0!),一半(9.0)应该返回4.5,一半("十七")应该返回"seve",一半((1,2,3,4,5, 6,7))应该返回(1,2,3).我试过这段代码没有成功:

def half(x):
    """returns half of the input, rounded down

    str -> str, int -> int, float -> float, tuple -> tuple"""
    return int(x/2)
    if x is float:
        return float(x/2)
    if x is tuple:
        return tuple(x/2)
    if x is str:
        return str(x/2)
Run Code Online (Sandbox Code Playgroud)

python python-3.x

1
推荐指数
1
解决办法
691
查看次数

在类中键入错误

我正在尝试创建一个Thing被调用的子类Openable.在定义__init__()方法时,我得到了一个

type error: Traceback (most recent call last):
  File "/Users/jaredbanton8/Documents/things.py", line 57, in <module>
    book1 = Openable("Necronomicon", "book shelf")
  File "/Users/jaredbanton8/Documents/things.py", line 40, in __init__
    super().__init__(name)
TypeError: __init__() missing 1 required positional argument: 'location'
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我已经为我的类包含了测试.我不确定是什么导致了这个错误.我的代码是:

    class Thing:
        """a class for representing physical objects in a game

        attributes: name (str), location (str)"""
        def __init__(self, name, location):
            """assigns values to attributes"""
            self.name = name
            self.location = location

        def description(self):
            """returns str that describes the state of …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

1
推荐指数
2
解决办法
99
查看次数

int 和 str 的乘法

我对这个看似简单的问题感到完全困惑。我想定义一个接受输入的函数,输入可以是 str 或 int 并将其加倍。例如,如果输入为'foo',则输出将为'foofoo'。同样,如果输入是 (9),输出将为 18。有人能指出我正确的方向吗?

这是我到目前为止所拥有的:

def double(x):
    """returns two times a number if input is a number, returns double the size of the string inputted, if input is a string
str -> str; int -> int"""
    if x is :
        return x*2
    if x is :
        return x*2
Run Code Online (Sandbox Code Playgroud)

python python-3.x

0
推荐指数
1
解决办法
1821
查看次数

TypeError与函数

我正在尝试定义一个函数,该函数返回没有该列表中第一个和最后一个项的列表.但是,当我运行该函数时,我得到了这个:"TypeError:'builtin_function_or_method'对象不支持删除项目".

到目前为止这是我的代码:

def middle(t):
    """returns a copy of a list with the first and last items removed

    list -> list"""
    t = input
    del t[0]
    del t[-1]
    print(t)
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

python python-3.x

0
推荐指数
1
解决办法
229
查看次数

标签 统计

python ×5

python-3.x ×5