我创建了一个两级菜单(请参阅下面的代码),但工作不正常。
我注意到我必须输入两次数字才能工作。并且第二级菜单不起作用
我想知道你是否能帮忙。
谢谢
def main():
choice =''
while choice !='0':
choice = input ('Main choice: type: nchoice 1: nchoice 2: nchoice 3: nchoice 4: nchoice 5: nchoice 6: ')
print ('main menu 0')
if choice =='1':
print ('choice 1 - first level')
choice = input ('Main choice: type: nchoice 1: nchoice 2:nchoice 3 to go back:')
if choice =='1':
print ('choice 1:1 - end level menu')
choice = input ('Main choice: type: nchoice 1: nchoice 2:nchoice 3 to go back:')
elif choice =='2':
print ('choice 2:1 - end level menu')
choice = input ('Main choice: type: nchoice 1: nchoice 2:nchoice 3 to go back:')
elif choice =='3':
break
elif choice =='2':
print ('choice 2- first level')
choice = input ('Main choice: type: nchoice 1: nchoice 2: nchoice 3: nchoice 4: nchoice 5: nchoice 6: ')
elif choice =='3':
print ('choice 3- first level')
choice = input ('Main choice: type: nchoice 1: nchoice 2: nchoice 3: nchoice 4: nchoice 5: nchoice 6: ')
elif choice =='4':
print ('choice 4 - first level')
choice = input ('Main choice: type: nchoice 1: nchoice 2: nchoice 3: nchoice 4: nchoice 5: nchoice 6: ')
elif choice =='5 - first level':
print ('choice 5')
joice = input ('Main choice: type: nchoice 1: nchoice 2: nchoice 3: nchoice 4: nchoice 5: nchoice 6: ')
main()
在玩了你的代码之后,我能够做出一些更改,希望它们能被理解。一旦你定义了一个函数,你就必须调用它。所以仅仅定义main
是不够的,你必须在代码中通过main()
来调用它
对于选项5,您希望有一个选项转到第二个菜单。所以我定义了另一个函数,并称之为second_menu()
。我打印了一些独特的东西,这样你就可以知道你去了菜单。唯一的问题是循环中断,但如果你想让用户留在循环中,这是可以解决的。看看新代码:
def main():
choice ='0'
while choice =='0':
print("Main Choice: Choose 1 of 5 choices")
print("Choose 1 for something")
print("Choose 2 for something")
print("Choose 3 for something")
print("Choose 4 for something")
print("Choose 5 to go to another menu")
choice = input ("Please make a choice: ")
if choice == "5":
print("Go to another menu")
second_menu()
elif choice == "4":
print("Do Something 4")
elif choice == "3":
print("Do Something 3")
elif choice == "2":
print("Do Something 2")
elif choice == "1":
print("Do Something 1")
else:
print("I don't understand your choice.")
def second_menu():
print("This is the second menu")
main()
尝试复制、粘贴并运行此代码,看看它是否更接近您想要的内容。另外,请花点时间注意if/elif/else
逻辑是如何工作的。如果你还有其他问题,请告诉我。
我不确定您想用代码实现什么。我甚至把它粘贴在IDLE中,但我仍然不确定。不过不用担心,我是来帮忙的。我也创建了一些菜单。下面是我的一个例子:
while loop_condition == True:
print("nWelcome to Python Calculator!n")
print("nPlease enter a number for what you want to do.n")
print("Enter 1 for addition.")
print("Enter 2 for subtaction.")
print("Enter 3 for division.")
print("Enter 4 for multiplication.")
print("Enter 5 to quit.n")
main_input = int(input("What would you like to do? "))
然后我用条件逻辑来遵循它。
if main_input == 5:
loop_condition = False
break
else:
num1 = int(input("nEnter your first number: "))
num2 = int(input("Enter your sencond number: "))
if main_input == 1:
print(add(num1,num2))
elif main_input == 2:
print(sub(num1,num2))
elif main_input == 3:
print(div(num1,num2))
elif main_input == 4:
print(multi(num1,num2))
通过在几行中打印出选择,然后在一个单独的变量中处理输入,我能够保持代码相当干净。如果这不能解决您的担忧,或者至少在某种程度上有所帮助,请告诉我。
#------------------------------------------------------------------------
# Name: menu.py
# Purpose: Despliega un menu guardado en un diccionario
# y ejecura la funcion correspondiente a cada entrada
# del diccionario
#
def hace_La_0():
return 'hice la cero'
def hace_La_1():
return 'hice la uno'
def hace_La_2():
return 'hice la dos'
def hace_La_f():
return 'hice la f. Me salgo'
def llama_la(opcion='f'):
# Obtiene la funcion del diccionario
func = opciones[opcion]['Funcion']
## print(func)
## Enter_continuar()
# Ejecuta la function
return func()
def cls():
import os
_=os.system("cls")# en windows
## _=os.system("clear") # en unix linux
## print ("n" * 25) #pseudo limpiar la pantalla sin os solo la desplaza
def menu(hecho='',s=25):
print('Estas son las opciones disponibles del programa:','n','-'*s)
for op in opciones:
print('t',op,' : ',opciones[op]['Des'])
print('-'*s,'Ya realice->',hecho)
op=input('Opcion--->').lower()
if op in opciones:
print(llama_la(opcion=op))
if op=='f':
return op
else:
print('Opcion inexistente---',op,'---intente de nuevo')
_=imput('Enter para continuar-->')
return ''
return op
opciones ={
'0' : {'Des': 'La opcion 0 hace esto y lo otro y lo de mas alla', 'Funcion': hace_La_0, 'Param1':'p1','Param2' :'p2'},
'1' : {'Des': 'La opcion 1 hace esto y lo otro y lo de mas alla', 'Funcion': hace_La_1, 'Param1':'p1','Param2' :'p2'},
'2' : {'Des': 'La opcion 2 hace esto y lo otro y lo de mas alla', 'Funcion': hace_La_2, 'Param1':'p1','Param2' :'p2'},
'f' : {'Des': 'Terminar', 'Funcion': hace_La_f, 'Param1':'p1','Param2' :'p2'}
}
op,hecho,s='','',25
while op!='f':
op=menu(hecho,s)
hecho=hecho +' ' + op
cls()
print('-'*s,'Fin del programa: realicé:',hecho)
最初的问题已经回答,但您可能需要考虑在此处使用控制台菜单