将字典放入 if 语句时出现类型错误
Type Error when putting dictionary in a if statement
下面的代码很好(据我所知),但我认为它可能会为下半部分添加更多上下文
IncAsk = bool(True)
while IncAsk:
try:
Income = {'Jan': int(input("How much money did you make in January? ")),
'Feb': int(input("How much money did you make in February? ")),
'Mar': int(input("How much money did you make in March? ")),
'Apr': int(input("How much money did you make in April? ")),
'May': int(input("How much money did you make in April? ")),
'Jun': int(input('How much money did you make in June? ')),
'Jul': int(input("How much money did you make in July? ")),
'Aug': int(input('How much money did you make in August? ')),
'Sep': int(input('How much money did you make in September? ')),
'Oct': int(input('How much money did you make in October? ')),
'Nov': int(input('How much money did you make in November? ')),
'Dec': int(input('How much money did you make in December? '))}
except ValueError:
print('Oops! It seems you have made a mistake. Numbers only please. Sorry for the inconvenience.')
但这段代码的问题在于,它应该检查整个字典并查看每个个体是否为正数,如果它们都是正数,它将打印一条消息并继续。如果不是,它将重复直到用户正确为止。但是当我 运行 它停止并给了我一个 'type error'。上半部分运行良好,一旦达到此值就会崩溃。我已经为 if 语句尝试了多种不同的格式,所以我很确定问题更深层次,可能是上半部分的问题。
if Income(['Jan'], ['Feb'], ['Mar'], ['Apr'], ['May'], ['Jun'], ['Jul'], ['Aug'], ['Sep'], ['Oct'], ['Nov'], ['Dec']) > -1:
print('Looks like everything is ok, now on to spending')
IncAsk = bool(False)
Income
不是函数,不能写成Income(...)
。您使用 Income[...]
.
访问元素
您不能通过一次放置所有下标来测试多个字典元素。如果要测试多个元素,可以使用all()
函数:
if all(val > -1 for val in Income.values()):
顺便说一句,让用户重新键入 所有 输入时,当他们输入错误时,这似乎是糟糕的设计。最好在输入时检查每个输入,在输入正确之前不要继续下一个输入。我建议你写一个单独的函数来获取满足所有条件的输入,并在分配给字典时调用它。
下面的代码很好(据我所知),但我认为它可能会为下半部分添加更多上下文
IncAsk = bool(True)
while IncAsk:
try:
Income = {'Jan': int(input("How much money did you make in January? ")),
'Feb': int(input("How much money did you make in February? ")),
'Mar': int(input("How much money did you make in March? ")),
'Apr': int(input("How much money did you make in April? ")),
'May': int(input("How much money did you make in April? ")),
'Jun': int(input('How much money did you make in June? ')),
'Jul': int(input("How much money did you make in July? ")),
'Aug': int(input('How much money did you make in August? ')),
'Sep': int(input('How much money did you make in September? ')),
'Oct': int(input('How much money did you make in October? ')),
'Nov': int(input('How much money did you make in November? ')),
'Dec': int(input('How much money did you make in December? '))}
except ValueError:
print('Oops! It seems you have made a mistake. Numbers only please. Sorry for the inconvenience.')
但这段代码的问题在于,它应该检查整个字典并查看每个个体是否为正数,如果它们都是正数,它将打印一条消息并继续。如果不是,它将重复直到用户正确为止。但是当我 运行 它停止并给了我一个 'type error'。上半部分运行良好,一旦达到此值就会崩溃。我已经为 if 语句尝试了多种不同的格式,所以我很确定问题更深层次,可能是上半部分的问题。
if Income(['Jan'], ['Feb'], ['Mar'], ['Apr'], ['May'], ['Jun'], ['Jul'], ['Aug'], ['Sep'], ['Oct'], ['Nov'], ['Dec']) > -1:
print('Looks like everything is ok, now on to spending')
IncAsk = bool(False)
Income
不是函数,不能写成Income(...)
。您使用 Income[...]
.
您不能通过一次放置所有下标来测试多个字典元素。如果要测试多个元素,可以使用all()
函数:
if all(val > -1 for val in Income.values()):
顺便说一句,让用户重新键入 所有 输入时,当他们输入错误时,这似乎是糟糕的设计。最好在输入时检查每个输入,在输入正确之前不要继续下一个输入。我建议你写一个单独的函数来获取满足所有条件的输入,并在分配给字典时调用它。