TypeError:“非类型”对象在函数中不可下标

2024-04-25 08:40:29 发布

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

下面的(第一次尝试)脚本是对我的一个测试,如何使用一个函数的返回值作为另一个函数的输入值。 这很好: 用户可以选择D或W(存款/取款)。 saldo_实际值(目前)为1500。 出于评估原因,我在功能输入控制和功能财务操作之间打印(值)。 这些值确实在第二个函数中得到

带D、W的输入结果位于代码下方(以及TypeError)

然而!问题>&燃气轮机;如果输入为空5次,或给出了其他字母D或W,则名为:input_control的函数中作为输入的第一个值为None。这就产生了一个打字错误。我尝试了不同的方法,但我想不出解决办法。 希望你能帮助我。非常感谢!!你好,简

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

Tags: andinactionsforiscounteremptyprint
1条回答
网友
1楼 · 发布于 2024-04-25 08:40:29

如果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
网友
2楼 · 发布于 2024-04-25 08:40:29

由于“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)

相关问题 更多 >