【发布时间】:2021-10-22 03:14:41
【问题描述】:
下面的(第一次尝试-)脚本是对我的一个测试,如何将一个函数的返回值用作另一个函数的输入值。 这工作正常: 用户可以选择 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
【问题讨论】:
-
很明显
input_control正在经历一条它没有return任何东西的路径...所以我建议使用调试器逐步调试您的程序,例如在PyCharm,以便准确确定正在采用的代码路径,以便您可以看到事情从哪里开始表现出与您预期不同的行为。这里的人可以为你调试你的代码,但你真的应该在发帖之前自己做。如果您已经调试过,请包含该信息。 -
在这种情况下,您的函数不会显式返回任何内容,并且不返回任何内容等同于返回
None。使用前需要检查返回值。 -
Random Davis 感谢您的建议。好点子。我将开始使用调试器。一月
-
重点是
input_control可能返回无。所以你有两个选择:1)确保它永远不会返回 None(更改函数的实现) 2)确保调用者代码会检查返回值