我有变量d int。
filter
但是如果您没有输入数字,那么程序会因错误而崩溃。为了避免这种情况,我写了这个:
collect
limit (0)
map
filter
stream (returns 1)
/filter (false)
filter
stream (returns 2)
/filter (true)
/map (returns 4)
/limit (1)
limit (1)
map
filter
stream (returns 3)
/filter (false)
filter
stream (returns 4)
/filter (true)
/map (returns 16)
/limit (2)
limit (2)
/limit (no more items; limit reached)
/collect
但收到错误
SyntaxError:语法无效。
答案 0 :(得分:2)
d = input("1 - to enter expression; 2 - to enter text; 3 - to exit. ")
try:
d = int(d)
except ValueError:
d = 3 # the default value