【发布时间】:2011-05-22 09:45:49
【问题描述】:
我为我的物理章节编写了一个程序来解决问题,它获取所有给定的数据并尽其所能。我使用了一长串 if 语句来检查哪些函数可以安全调用(函数本身并不安全),但我觉得必须有更好的方法来做到这一点。
完整代码是here
这是违规者的 sn-p(argparse 默认为 None):
# EVALUATE:
if args.t and args.ld:
print 'Velocity:', find_velocity(args.t, args.ld)
if args.t and args.l and args.m:
print 'Velocity:', find_velocity(args.t, args.l, args.m)
if args.l:
print 'Longest possible standing wave length:', find_longest_possible_standing_wave_length(args.l)
if args.l and args.m and args.t and args.n:
print 'Frequency of the standing wave with', args.n, 'nodes:', find_nth_frequency_standing_wave(args.t, args.n, args.l, args.m)
if args.s and args.t and args.n and args.l:
print 'Frequency of', args.n, 'standing wave:', find_nth_frequency_standing_wave(args.t, args.n, args.l, velocity=args.s)
if args.ld and args.t and args.f:
print 'Angular wave number: ', find_angular_wave_number(args.ld, args.t, args.f)
if args.p:
print 'Difference in amplitude of twins:', find_amplitude_difference_of_twins(args.p)
if args.f:
print 'Angular wave frequency:', find_angular_wave_frequency(args.f)
谢谢!
【问题讨论】:
-
在这里获取您的代码。它有帮助。
-
“安全”? “不安全”?什么意思?
-
我相信他的意思是抛出异常。
-
@S.Lott,不要尝试/捕获,他们会接受垃圾值并继续推进,直到出现问题。
-
“往前推进,直到事情搞砸了”?你的意思是直到他们得到例外?那有什么问题?这就是事情应该运作的方式。请更新问题以明确定义“安全”和“不安全”。
标签: python arguments switch-statement exists