我怎么能做我的代码,所以我不必使用所有的def,if和elif语句 [英] How can I do my code so that I dont have to use all the def , if and elif statements
本文介绍了我怎么能做我的代码,所以我不必使用所有的def,if和elif语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
menu_Cheap = [['1. Shark ',4.10,], ['2. Flounder',4.10,], ['3. Cod ',4.10,], ['4. Gurnet ',4.10,], ['5. kahawai ',4.10,], ['6. Trevall ',4.10,]]
menu_Deluxe = [['7. Snapper ',7.20,], ['8. Pink Salmon ',7.20,], ['9. Tuna ',7.20,], ['10.Smoked Marlin',7.20,], ['11.Clown fish ',7.20,], ['12.Surgeonfish ',7.20,]]
DELIVERY = 5
PICKUP = -1.05
MAX_FISH = 7
print('\nThese are the menu items and their prices \n')
for x in menu_Cheap:
print(x)
for y in menu_Deluxe:
print(y)
def Shark():
print("you have choosen Shark ")
def Flounder():
print("you have choosen Flounder ")
def Cod():
print("you have choosen Cod ")
def Gurnet():
print("you have choosen Gurnet ")
def Kahawai():
print("you have choosen Kahawai ")
def Trevally():
print("you have choosen Trevally ")
def Snapper():
print("you have choosen Snapper ")
def Pink_Salmon():
print("you have choosen Pink Salmon ")
def Tuna():
print("you have choosenTuna ")
def Smoked_Marlin():
print("you have choosen Smoked_Marlin ")
def Clown_Fish():
print("you have choosen Clown fish ")
def Surgeonfish():
print("you have choosen Surgeonfish ")
counter = 0
loop = int(input("\nPlease enter the total amount of fish you would like to order\n"))
while counter < loop :
try:
selection = int(input("\nPlease enter the type of fish you would like to order\n"))
if selection == 1:
Shark()
counter = counter + 1
elif selection == 2:
Flounder()
counter = counter + 1
elif selection == 3:
Cod()
counter = counter + 1
elif selection == 4:
Gurnet()
counter = counter + 1
elif selection == 5:
Kahawai()
counter = counter + 1
elif selection == 6:
Trevally()
counter = counter + 1
elif selection == 7:
Snapper()
counter = counter + 1
elif selection == 8:
Pink_Salmon()
counter = counter + 1
elif selection == 9:
Tuna()
counter = counter + 1
elif selection == 10:
Smoked_Marlin()
counter = counter + 1
elif selection == 11:
Clown_Fish()
counter = counter + 1
elif selection == 12:
Surgeonfish()
counter = counter + 1
else:
print("please enter a values between 1 and 12 ")
except ValueError:
print("No strings ")
exit
print (Surgeonfish())
我尝试了什么:
我的代码非常低效,并且想知道是否有人可以给我一些想法
What I have tried:
my code is very inefficient and was wondering if anyone can give me some ideas
推荐答案
见这里:如何在Python中实现switch-case语句 - JAXenter [ ^ ]
或者对于高级用户,请参阅Paul Rubin的解决方案:如何创建函数数组? - Python [ ^ ]
See here: How to implement a switch-case statement in Python - JAXenter[^]
Or for advanced users, see Paul Rubin's solution here: How do I create an array of functions? - Python[^]
这是不完整的,但展示了如何在没有所有额外定义的方法或if / elif语句的情况下管理列表。
This is incomplete, but shows how to manage a list without all the extra defined methods, or the if/elif statements.
menu = [['1. Shark ',4.10,], ['2. Flounder',4.10,], ['3. Cod ',4.10,], ['4. Gurnet ',4.10,], ['5. kahawai ',4.10,], ['6. Trevall ',4.10,],['7. Snapper ',7.20,], ['8. Pink Salmon ',7.20,], ['9. Tuna ',7.20,], ['10.Smoked Marlin',7.20,], ['11.Clown fish ',7.20,], ['12.Surgeonfish ',7.20,]]
DELIVERY = 5
PICKUP = -1.05
MAX_FISH = 7
print('\nThese are the menu items and their prices \n')
for x in menu:
print(x)
counter = 0
loop = int(input("\nPlease enter the total amount of fish you would like to order\n"))
while counter < loop :
try:
selection = int(input("\nPlease enter the type of fish you would like to order\n"))
if selection < 1 or selection > len(menu):
print("Invalid selection please try again")
else:
print("You have chosen", menu[selection -1][0])
except ValueError:
print("Please enter a number between 1 and " + len(menu))
exit
这篇关于我怎么能做我的代码,所以我不必使用所有的def,if和elif语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文