• <i id='JImIT'><tr id='JImIT'><dt id='JImIT'><q id='JImIT'><span id='JImIT'><b id='JImIT'><form id='JImIT'><ins id='JImIT'></ins><ul id='JImIT'></ul><sub id='JImIT'></sub></form><legend id='JImIT'></legend><bdo id='JImIT'><pre id='JImIT'><center id='JImIT'></center></pre></bdo></b><th id='JImIT'></th></span></q></dt></tr></i><div id='JImIT'><tfoot id='JImIT'></tfoot><dl id='JImIT'><fieldset id='JImIT'></fieldset></dl></div>

      <legend id='JImIT'><style id='JImIT'><dir id='JImIT'><q id='JImIT'></q></dir></style></legend>

      • <bdo id='JImIT'></bdo><ul id='JImIT'></ul>
    1. <tfoot id='JImIT'></tfoot>

      <small id='JImIT'></small><noframes id='JImIT'>

      1. TypeError:列表索引必须是整数或切片,而不是浮点数

        TypeError: list indices must be integers or slices, not float(TypeError:列表索引必须是整数或切片,而不是浮点数)
        <tfoot id='yg7F6'></tfoot>

            <tbody id='yg7F6'></tbody>

            <bdo id='yg7F6'></bdo><ul id='yg7F6'></ul>
            <legend id='yg7F6'><style id='yg7F6'><dir id='yg7F6'><q id='yg7F6'></q></dir></style></legend>

                1. <small id='yg7F6'></small><noframes id='yg7F6'>

                2. <i id='yg7F6'><tr id='yg7F6'><dt id='yg7F6'><q id='yg7F6'><span id='yg7F6'><b id='yg7F6'><form id='yg7F6'><ins id='yg7F6'></ins><ul id='yg7F6'></ul><sub id='yg7F6'></sub></form><legend id='yg7F6'></legend><bdo id='yg7F6'><pre id='yg7F6'><center id='yg7F6'></center></pre></bdo></b><th id='yg7F6'></th></span></q></dt></tr></i><div id='yg7F6'><tfoot id='yg7F6'></tfoot><dl id='yg7F6'><fieldset id='yg7F6'></fieldset></dl></div>

                  本文介绍了TypeError:列表索引必须是整数或切片,而不是浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  def convBin():
                      cont = []
                      rest = []
                      dev = []
                      decimal = []
                  
                      print("Ingrese el valor a convertir: ")
                      valor = ast.literal_eval(input())
                  
                      if isinstance(valor, int):
                          while valor > 0:
                                  z = valor // 2
                              resto = valor%2
                              valor = valor // 2
                              cont.append(z)
                              rest.append(resto)
                  
                          cont.reverse()
                          rest.reverse()
                  
                          dev.append(cont[0])
                  
                          x = 0
                          while x <= (len(rest) - 1):
                              dev.append(rest[x])
                              x += 1
                  
                          print(" ")
                          print("Lista de devoluciones: ")
                          print(dev)
                          print("")
                  
                      elif isinstance(valor, float):
                          a = valor // 1
                          b = valor % 1
                  
                          while a > 0:
                              z = a // 2
                              resto = a%2
                              a = a // 2
                              cont.append(z)
                              rest.append(resto)
                  
                          cont.reverse()
                          rest.pop()
                  
                          dev.append(cont[1])
                  
                          for i in rest:
                              dev.append(rest[i])
                  
                          print("Inserte el número de error minimo")
                          num = input()
                  
                          while num > 0:
                              dec = b * 1
                              dec2 = dec//1
                              dec %= 1        
                              decimal.append(dec2)
                  
                  
                          print("Parte entera: ")
                          print(dev)
                          print("Parte decimal:")
                          print(num)
                  
                      else:
                          print("Ha aparecido un error")
                  

                  Its shows me an error that i cant append a float into a list.

                  After asking you a number, it controls which type of number is it. When it is an int, it doesnt have any problem. But when its a float, it says that it cant add a float into a list where its saved the numbers of the operations made before.

                  Can someone explain it to me why I cant append floats into a list or how can I solve the problem?

                  Traceback (most recent call last): File "Converter.py", line 169, in convBin(); File "Converter.py", line 53, in convBin dev.append(rest[i]) TypeError: list indices must be integers or slices, not float

                  Thanks.

                  解决方案

                  for i in rest will give you the actual item in the list, and not index. From your code, it seems that you want to append the value. But actually, you are treating the value as index again, and trying to fetch it from the array.

                  for i in rest:
                              dev.append(rest[i])
                  

                  Fix:

                  Just change above to:

                  dev.extend(rest)
                  

                  But this code, takes a value from rest, then again uses that value as an index, and if that value i turns out to be a float, it throws an exception.

                  You haven't mentioned which line gives you this error. But I think it must be this one. It might give many other unexpected errors like array out of bound, etc

                  This is the error I get, if I run your code for valor = 18.5

                  https://ideone.com/HGagLb

                  Traceback (most recent call last): File "./prog.py", line 71, in File "./prog.py", line 51, in convBin TypeError: list indices must be integers or slices, not float

                  Difference between the above example, and the one below(from your code, where you handle int):

                  x = 0
                  while x <= (len(rest) - 1):
                      dev.append(rest[x])
                      x += 1
                  

                  is that, in the first case, i is actually the item(int or float) in the list rest, while in the later one, it's a valid index.

                  这篇关于TypeError:列表索引必须是整数或切片,而不是浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                        <bdo id='UmnV6'></bdo><ul id='UmnV6'></ul>

                              <tbody id='UmnV6'></tbody>

                            <i id='UmnV6'><tr id='UmnV6'><dt id='UmnV6'><q id='UmnV6'><span id='UmnV6'><b id='UmnV6'><form id='UmnV6'><ins id='UmnV6'></ins><ul id='UmnV6'></ul><sub id='UmnV6'></sub></form><legend id='UmnV6'></legend><bdo id='UmnV6'><pre id='UmnV6'><center id='UmnV6'></center></pre></bdo></b><th id='UmnV6'></th></span></q></dt></tr></i><div id='UmnV6'><tfoot id='UmnV6'></tfoot><dl id='UmnV6'><fieldset id='UmnV6'></fieldset></dl></div>

                            <small id='UmnV6'></small><noframes id='UmnV6'>

                            <tfoot id='UmnV6'></tfoot><legend id='UmnV6'><style id='UmnV6'><dir id='UmnV6'><q id='UmnV6'></q></dir></style></legend>