Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数中的变量,或在else语句后返回值_Python - Fatal编程技术网

Python 函数中的变量,或在else语句后返回值

Python 函数中的变量,或在else语句后返回值,python,Python,我是Python新手,我的函数没有按预期工作。我试图将一些值保存在一个列表alt[i]中,但是如果我调用if或else语句之外的值,我只会得到一个值 import os.path import xlrd import sys import argparse def readExcel(): workbook = xlrd.open_workbook(path) sheet = workbook.sheet_by_index(0) sheet = workbook.

我是Python新手,我的函数没有按预期工作。我试图将一些值保存在一个列表alt[i]中,但是如果我调用if或else语句之外的值,我只会得到一个值

import os.path
import xlrd
import sys
import argparse

def readExcel():
     workbook = xlrd.open_workbook(path)
     sheet = workbook.sheet_by_index(0)
     sheet = workbook.sheet_by_index(0)
     j=0
     for row in range(sheet.nrows):
          j += 1
     rownumber=j
     print (rownumber)

     k=0
     i=0
     list_alt=[None]*j 
     list_neu=[None]*j
     while (i<j-10):
          #get first row values
          temp = sheet.cell_value(k,0).replace('www.website.com/','')   
          #get second row values
          temp2 = sheet.cell_value(k,1)
          if "http:" in temp2:
               temp2 = sheet.cell_value(k,1).replace('http:','https:')
               list_neu[i]=temp2
               list_alt[i]=temp
               k+=0
               i+=1
          else:
               list_neu[i]=temp2
               list_alt[i]=temp
               #i can print the values here
               print (list_alt[i])
               print (list_neu[i]) 
               i+=1
               k+=1

          #I cant print for the values here anymore
          print (list_alt[i])
          print (list_neu[i])
          print (i)

     return list_alt, list_neu
如果您得到了IndexError,则应该是由于在使用索引列表之前增加了i_alt[i]引起的

我希望我的回答能有所帮助D

超过if/else的行在i递增后执行,因此它当然不会打印任何行,因为这确实是list_alt[i]的值。不管怎样,函数都应该返回正确的值。如果您希望看到刚才插入的值,可以考虑两种可能的变化:

增量i作为while循环中的最后一行 打印列表替换为[i-1]
缩进在Python中非常重要。请格式化代码以反映实际缩进。我更新了代码以反映实际缩进。它实际上在else语句中,我可以打印我的值,但在它之外,列表是空的
import os.path
import xlrd
import sys
import argparse

def readExcel():
workbook = xlrd.open_workbook(path)
sheet = workbook.sheet_by_index(0)
sheet = workbook.sheet_by_index(0)
j=0
for row in range(sheet.nrows):
    j += 1
rownumber=j
print (rownumber)

k=0
i=0
list_alt=[None]*j 
list_neu=[None]*j
while (i<j-10):
    temp = sheet.cell_value(k,0).replace('www.website.com/','') 
    temp2 = sheet.cell_value(k,1)
    if "http:" in temp2:
        temp2 = sheet.cell_value(k,1).replace('http:','https:')
        list_neu[i]=temp2
        list_alt[i]=temp
        k+=0
        # variable i is increased.
        i+=1
    elif "http" not in temp2:
        print ("not there")
        k+=1
    else:
        list_neu[i]=temp2
        list_alt[i]=temp
        print (list_alt[i])
        print (list_neu[i]) 
        # variable i is increased.
        i+=1
        k+=1

    # variable i is increased before.
    # you may purpose to read list_alt[i-1].
    print (list_alt[i])
    print (i)

return list_alt, list_neu