我編寫了一個程序來解決我的物理章節中遇到的問題,該章節採用所有給定的數據並盡其所能完成所有工作。我使用了一串長長的if語句來檢查哪些函數可以安全地調用(函數本身並不安全),但是我覺得必須有更好的方法來實現。根據輸入Python的不同功能
完整的代碼是here
這裏的罪犯的片段(argparse默認爲無):
# 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)
謝謝!
獲取您的代碼在這裏。它有助於。 – pyfunc 2010-12-08 18:14:44
「安全」? 「不安全」?你的意思是什麼? – 2010-12-08 18:15:19
我相信他意味着拋出異常。 – marr75 2010-12-08 18:19:10