【发布时间】:2012-03-12 12:36:01
【问题描述】:
所以我有代码:
def logdata(x, y):
try:
f = open('multlog.txt', 'a')
f.write("{0:g} * {1:g} = {2:g}\n".format(x,y, (x*y)))
except ValueError:
f.write("Error, you tried to multiply by something that wasn't a number")
raise
finally:
f.close()
print("This is a test program, it logs data in a text file, 'multlog.txt'")
fn = input("Enter the first number you'd like to multiply by: ")
sn = input("Enter the second number you'd like to multiply by: ")
logdata(int(fn), int(sn))
我想要它做的是,当它到达一个值错误时,它会写入文件,“错误,你试图乘以一个不是数字的东西”。但是,如果用户输入一个字母,比如“j”,ValueError: invalid literal for int() with base 10: 'j',文件达到值错误,它就不会写入文件!
【问题讨论】:
标签: python-3.x