python - TypeError:“NoneType”对象在函数中不可下标
问题描述
以下(第一次尝试-)脚本对我来说是一个测试,如何使用一个函数的返回值作为另一个函数的输入值。这很好用:用户可以选择 D 或 W(存款/取款)。saldo_actual(目前)为 1500。出于评估原因,我在函数 input_control 和函数 finance_actions 之间打印(值)。这些值确实在第二个函数中。
使用 D、W 输入的结果在代码下方(以及 TypeErro)
然而!问题>> 如果输入为空 5 次,或者给出其他字母 D 或 W,则在名为 input_control 的函数中作为输入的 value 的第一个值是 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
解决方案
它抛出类型错误,因为“值”的值为无。为避免出现此错误,请尝试-
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)
推荐阅读
- ios - 文本未包含在 swift UI 中
- javascript - 页面加载时如何加载页面内容
- python - 如何将计数器集合转换为列表
- c# - ADFS 自定义身份验证提供程序元数据不匹配
- python-3.x - 如何从另一个线程中的同步代码调用异步代码?
- java - Zuul 网关异常中的 java.net.URI 编码异常
- python-3.x - 我想点击一个只有属性 href="javascript: GFESubmitForm('add-credit-report-quotation-report-details.do')" 的标签
- java - 将 ArrayList 上的 Json 数据传递给新活动的 RecyclerView 加载数据延迟
- symfony - 解压 symfony/symfony
- excel - 如何按 IF 拆分数据