*:“float”和“NoneType”的操作数类型不受支持

2024-06-26 14:29:47 发布

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

在运行代码时会出现这样的错误

unsupported operand type(s) for *: 'float' and 'NoneType'

如果我删除这部分

Total = Total* exp
    Total = 1-Total
    possition = possition + 1 

有这样的错误吗

IndexError: index 4 is out of bounds for axis 1 with size 4

代码:

import random

def getsys():
    row = ''
    for i in range(0 , 8):
        randintt = str(random.randint(0 , 4))
        row += randintt
    return row


def getx():
    x = []
    for i in range(0,14):
        mysys = getsys()
        x.append(mysys)

    return x 

y = getx()
print (y)
import initialsys
import numpy as np

R = np.array([[0.90 , 0.93,0.91 , 0.95],
               [0.95 , 0.94, 0.93, 0],
               [0.85 , 0.90 , 0.87 , 0.92],
               [0.83 , 0.87 , 0.85 , 0 ],
               [0.94 , 0.93 , 0.95 , 0],
               [0.99 , 0.98 , 0.97 , 0.96],
               [0.91 , 0.92 , 0.94 , 0],
               [0.81 , 0.90 , 0.91 , 0],
               [0.97 , 0.99 , 0.96 , 0.91],
               [0.83 , 0.85 , 0.90 , 0],
               [0.94 , 0.95 , 0.96 , 0],
               [0.79 , 0.82 , 0.85 , 0.90],
               [0.98 , 0.99 , 0.97 , 0],
               [0.85 , 0.92 , 0.95 , 0.99]
              ])

def expression(r ,possition , char ):
    exp = 1-r[possition , int(char)]


x = initialsys.getx()
possition = 0
Total = 1
Total = float(Total)
char = ""
for row in x :
    for char in row :
        if char!= 0 :
            exp = expression(R , possition , char)
            Total = Total* exp
    Total = 1-Total
    possition = possition + 1 

Tags: 代码inimportfordef错误rangerandom
3条回答

一些变化。你知道吗

  • expression中缺少return语句
  • random.randint(0 , 4)应该是random.randint(0 , 3)

例如:

def expression(r ,possition , char ):
    exp = 1-r[possition , int(char)]
    return exp

def getsys():
    row = ''
    for i in range(0 , 8):
        randintt = str(random.randint(0 , 3))
        row += randintt
    return row

函数def expression(r ,possition , char )不返回任何内容。你知道吗

你应该这样写:

def expression(r ,possition , char ):
    return 1-r[possition , int(char)]

我将4更改为3,但IndexError:索引4超出轴1的界限,大小为4的错误仍然存在

相关问题 更多 >