TypeError: 'NoneType' object is not subscriptable in function

TypeError: 'NoneType' object is not subscriptable in function

以下(第一次尝试-)脚本是对我的测试,如何使用一个函数的return值作为另一个函数的输入值。 这很好用: 用户可以选择 D 或 W(存款/取款)。 saldo_actual(目前)为 1500。 出于评估原因,我在函数 input_control 和函数 finance_actions 之间打印(值)。 这些值确实进入了第二个函数。

D、W输入的结果在代码下方(还有TypeErro)

但是!问题 >> 如果输入为空 5 次,或者另一个字母然后给出 D 或 W,则函数中作为输入的值的第一个值:input_control,是 None。这给出了 TypeError。我尝试了不同的事情,但我无法找到解决方案。 希望你能帮助我。提前谢谢了!!问候一月

def input_control():
    
    # Actuele saldo na inleg en opnemen. Begin saldo_actual = 1000 euro.
    saldo_actual = 1500
    # saldo_deposit om aan het einde de klant het totaal gestorte bedrag te laten weten.
    saldo_deposit = 0
    # saldo_withdrawel om aan het einde de klant het totaal opgenomen bedrag te laten weten.
    saldo_withdrawel = 0
    # amount_total het totale verschil tussen saldo_deposit en saldo_withdrawel, in euro's.
    # amount_total wordt bij saldo_actual opgeteld (of afgetrokken.)
    amount_total = 0
    # empty_counter telt aantal keren dat het invoerveld leeg bleef PLUs de keren dat een foutief iets werd ingevoerd.
    attemps = 5
    # transactions_counter: aantal keren dat een transactie gedaan werd. Max = 5
    transactions_counter = 0
    # initieren van de mogelijkheid voor de klant om het programma te stoppen met een q of Q. stop.
    stop = 'a'
    #  saldo_minimum is ondergrens >> zit je aan de grens kan je niet meer opnemen.
    saldo_minimum = -1500
    empty_counter = 0
    letter_definitief = 'a'
      
    while empty_counter < 6:
        
        try:
        
            if empty_counter == 5:
                print("Je probeerde het 5 keer. Terminating Programm.")
                break

            letter_keuze= input('Wat wil je doen? Type D voor deponeren. W voor opnemen.')

            if not letter_keuze:  
                print('niks ingevuld.')
                print()
                empty_counter = empty_counter + 1
                continue


            if  letter_keuze.lower() != 'w' and letter_keuze.lower() != 'd':
                print('Type een W of D of Q als keuze. Nu typte je iets anders')
                print()
                empty_counter = empty_counter + 1
                continue

            if letter_keuze.lower() == 'd' or letter_keuze.lower() == 'w':
                   letter_definitief = letter_keuze.lower()
                    
            return letter_definitief, saldo_actual
                  
        except TypeError:
            print('it is a NoneTYpe')
            break
                     
value = input_control()  
print(value)

letter_definitief = value[0]
saldo_actual = value[1]

def finance_actions(*args):
    print(f'This is in finance_actions: {letter_definitief}.')
    print(f'This is also in finance_actions {saldo_actual}.')
  
finance_actions(letter_definitief, saldo_actual)

W 情况下的结果:

Choose D for Deposit and W for withdrawel.w
['w', 1500]
This is in finance_actions: w.
This is also in finance_actions 1500.

D 情况下的结果:

Choose D for Deposit and W for withdrawel.d
['d', 1500]
This is in finance_actions: d.
This is also in finance_actions 1500.

5 个空输入或 5 个其他字母的结果:

hoose D for Deposit and W for withdrawel.
There was nothing given in.

Choose D for Deposit and W for withdrawel.
There was nothing given in.

Choose D for Deposit and W for withdrawel.
There was nothing given in.

Choose D for Deposit and W for withdrawel.
There was nothing given in.

Choose D for Deposit and W for withdrawel.
There was nothing given in.

Choose D for Deposit and W for withdrawel.
You tried 5 times. Terminating Programm.
None

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-25-13cc824df307> in <module>
     56 print(value)
     57 
---> 58 letter_definitief = value[0]
     59 saldo_actual = value[1]
     60 

TypeError: 'NoneType' object is not subscriptable

它抛出类型错误,因为 'value' 的值是 None。 为避免出现此错误,请尝试-

if value != None:
 letter_definitief = value[0]
 saldo_actual = value[1]

 def finance_actions(*args):
    print(f'This is in finance_actions: {letter_definitief}.')
    print(f'This is also in finance_actions {saldo_actual}.')
  
 finance_actions(letter_definitief, saldo_actual)

如果 valueNone,那么你不应该是 运行 finance_actions。替换此代码:

letter_definitief = value[0]
saldo_actual = value[1]

def finance_actions(*args):
    print(f'This is in finance_actions: {letter_definitief}.')
    print(f'This is also in finance_actions {saldo_actual}.')
  
finance_actions(letter_definitief, saldo_actual)

使用此代码:

def finance_actions(*args):
    print(f'This is in finance_actions: {letter_definitief}.')
    print(f'This is also in finance_actions {saldo_actual}.')
  
if value:
    finance_actions(value)
else:
    #insert error message here