我正在尝试修改一个函数来管理文件结束异常错误。整数函数应该处理EOFError并打印一条消息并将None返回给main()。然后,必须相应地修改main函数以处理None值。我做了改变但不确定我是否走在正确的轨道上。有人可以给我一些指示吗?感谢
def integer( prompt ):
value_str = input( prompt )
try:
value_int = int( value_str )
except ValueError:
print ( "** Invalid input, assuming 0 **" )
value_int = 0
except EOFError:
print("End of file error occurred.")
return None
return value_int
def main():
try:
numer = integer( "Enter the numerator: " )
denom = integer( "Enter the denominator: " )
print( numer, "divided by", denom, end=" " )
result = numer/denom
print( "yields", result )
except ZeroDivisionError:
print( "** Invalid: attempted to divide by zero **" )
except EOFError:
print("unsupported operand type (s) for /: 'int' and 'NoneType'")
print( "Program halted" )
main()
答案 0 :(得分:0)
您的输入语句位于try块之外。将它移到块内&你应该好好去
def integer( prompt ):
try:
value_str = input( prompt )
...