我目前正在创建一个计算器,向用户询问一系列问题以计算其总和的答案,但是我很快就遇到了这样一个事实:我使用了多个if语句,这使得我的代码非常困难,厌倦阅读。
我想知道是否有另一种方法来使用多个if语句,每个语句都有不同的函数,占用的空间更少,完成相同的任务。
以下是我的代码示例:
def decision():
choice = input("""
Please Select an Option:
Type 1 for Addition, Subtraction, Multiplication and Division.
Type 2 for Squares or Square Roots, Cubes or Cube Roots, Powers, Roots and Percentages.
Type 3 for Greater Than and Less Than.
Type 4 for H.C.F(Highest Common Factor), G.C.D(Greatest Common Divisor) and L.C.M(Least Common Multiple).
Type 5 for Converting Degrees to Radians.
Type 6 for Calculating Grades from Marks.
Type 7 for Perimeter, Circumference, Area, Surface Area and Volume of Shapes.
Selection: """)
if choice == '1':
ASMD()
elif choice == '2':
SCERP()
elif choice == '3':
GL()
elif choice == '4':
HCF_GCD_LCM()
elif choice == '5':
DR()
elif choice == '6':
CG()
elif choice == '7':
PCASAV()
else:
print("You have not selected a Valid Option. Please run the program again.")
input("Press Enter to continue...")
exit()
提前致谢!