用户输入值时在python中传递值时出错

时间:2013-10-31 04:25:03

标签: python

def row_minimum(x,L):
  L=L
  if x=='1':
    row_minimum1=min(L[0],L[1],L[2],L[3],L[4])
    return row_minimum1
  elif x=='2':
    row_minimum2=min(L[5],L[6],L[7],L[8],L[9])
    return row_minimum2
  elif x=='3':
    row_minimum3=min(L[10],L[11],L[12],L[13],L[14])
    return row_minimum3
  table(L)



    def user_input(y):
        if y in ['1','2','3','A','B','C','D','E']:
            condition = False
        elif y !=['1','2','3','A','B','C','D','E']:
            condition = True
            while condition == True:
                z=input("Enter a row (as a number) or a column (as and uppercase letter):")
                if z in ['1','2','3','A','B','C','D','E']:
                    condition = False
            return z

def  menu(a,L):
  if a==1:
    display_table(L)
  elif a==2:
    x=input("Enter a row (as a number) or a column (as and uppercase letter):")
    user_input(x)
    print (user_input(x))
    if user_input(x) in ['1','2','3']:
        mini = row_minimum(x,l)
        print ("2")
        print("Minimum is:",row_minimum(x,L))

我得到user_input(x)的值为none,而我希望它从用户获取值并在if语句中进行比较并做最小值。

1 个答案:

答案 0 :(得分:0)

您的user_input()功能似乎是一个坏主意。据我所知,这是为了检查输入错误,并一直要求输入,直到它获得良好的输入。问题是,它只返回初始输入错误,如果初始输入良好,则函数不返回。

你可以完全摆脱这个功能,因为你最终还是会检查输入。你可以这样做:

def  menu(a,L):
    if a==1:
        display_table(L)
    elif a==2:
        while True:
            x=input("Enter a row (as a number) or a column (as and uppercase letter):")
            print x
            if x in ['1','2','3']:
                mini = row_minimum(x,l)
                print ("2")
                print("Minimum is:",row_minimum(x,L))
                break
            elif x in ['A','B','C','D','E']:
                whatever you want to do here
                break