如何在函数中将列表中的数字视为整数

2024-04-24 22:31:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图写一个函数,在给定位长度的情况下,找到有符号/无符号的max/min值。但是每次我执行这个函数都会遇到一个错误,这是我的代码

#function to find max and min number of both signed and unsigned bit values

    def minmax (n) :
        for numbers in n :

            unsignedmax = (2**n)-1
            unsignedmin = 0
            signedmax = 2**(n-1)-1
            signedmin = -2**(n-1)
        print "number     ", "unsigned              ", "signed"

    #Main

    bitlist = [2, 3, 8, 10, 16, 20, 32]

    minmax(bitlist)

错误是

^{pr2}$

我没有写完它,但运行它以确保逻辑部分没有错误,但我在试图查找值时遇到了一个错误。有没有一种方法可以插入int()或者类似的方法,把数字当作整数类型,而不是我假设的tyle list?在


Tags: and方法函数代码定位number错误符号
1条回答
网友
1楼 · 发布于 2024-04-24 22:31:38

将方法定义和第一行更改为:

def minmax (numbers) :
    for n in numbers :

也就是说,在这两行中,在出现的地方用“数字”替换“n”,在出现的地方用“n”替换“数字”。在

正如您所写的,变量“numbers”保存您要处理的列表中的项,而变量“n”保存列表。但其余的代码是用相反的假设编写的。在

相关问题 更多 >