当前位置: 首页 > 知识库问答 >
问题:

Python创建和遍历一个简单的菜单

郑宜民
2023-03-14

我正在创建我的第一个Python项目。我正在研究的是一个简单的基于文本的度量到标准(反之亦然)的转换程序。我不是在寻找代码,因为我想自己解决这个问题。但是,我在遍历菜单时遇到了一个问题。例如,有3个菜单:主菜单,公制菜单和标准菜单。程序打开到主菜单。在主菜单上,用户可以选择导航到metric菜单,standard菜单或退出程序。如果用户希望能够从度量或标准菜单返回到主菜单,最好的方法是什么?我试过while循环和if/elif。然而,我的代码似乎变得非常臃肿和复杂。有人能给我一些关于在Python中创建和遍历文本菜单的建议吗?

共有2个答案

郭弘方
2023-03-14

因为您说您是Python的新手,所以在第一次构造对象时调用类的__init_()方法。你可以在这里阅读关于类的文档(跳到9.3)。

我不知道这会有多大的效率,但是您可以使用一个存储同一类的其他对象的类:

class Menu:
    def __init__(self, content, short_view, submenus = None):
        self.content = content
        self.short_view = short_view
        if submenus != None:
            self.choices = dict(enumerate(submenus, 1))
            for sub in submenus:
                sub.parent = self
        else:
            self.choices = {}


subsub1 = Menu("this is subsub1 content", "this goes to subsub1")
subsub2 = Menu("this is subsub2 content", "this goes to subsub2")
subsub3 = Menu("this is subsub3 content", "this goes to subsub3")

sub1 = Menu("this is the first sub menu content", "this goes to sub1", [subsub1, subsub2, subsub3])
sub2 = Menu("this is the second sub menu content", "this goes to sub2")

main = Menu("this is the main menu content", "this goes to main, but will never be used", [sub1, sub2])
main.parent = main

current_menu = main
while True:
    print(current_menu.content)
    print("\n".join([f"[{num}] {current_menu.choices[num].short_view}" for num in current_menu.choices]))
    inpt = input("Choice: ")
    if inpt == "exit":
        break
    elif inpt == "back":
        current_menu = current_menu.parent
    else:
        current_menu = current_menu.choices[int(inpt)]

用法(在shell中):

this is the main menu content
[1] this goes to sub1
[2] this goes to sub2
Choice: 1
this is the first sub menu content
[1] this goes to subsub1
[2] this goes to subsub2
[3] this goes to subsub3
Choice: back
this is the main menu content
[1] this goes to sub1
[2] this goes to sub2
Choice: exit
>>>
谷梁子濯
2023-03-14

您可以尝试如下所示:

import sys
import os

def switch():
    clear = lambda: os.system('cls')
    clear()
    s = 0
    while s != 5:
        s = int(input('1)Metric 2)Standard 3)Exit\n'))
        if s == 1:
            clear = lambda: os.system('cls')
            clear()
            metric()
        elif s == 2:
            clear = lambda: os.system('cls')
            clear()
            standard()
        elif s == 3:
            clear = lambda: os.system('cls')
            clear()
            exit()
        else:
            print('Out of range.')

def metric():
    '''Your code here'''
    s = int(input('0)Back\n'))
    if s == 0:
        switch()
    else:
        print('Out of range.')
    
def standard():
    '''Your code here'''
    s = int(input('0)Back\n'))
    if s == 0:
        switch()
    else:
        print('Out of range.')
    

switch()




        
 类似资料:
  • 我试图创建一个简单的菜单,其中一个按钮将调用一个方法来清除数组。我不想使用xml,因为我只需要一个按钮。 像这样的东西- 非常感谢。

  • pre { white-space: pre-wrap; } 菜单(Menu)定义在一些 DIV 标记中,如下所示:     <div id="mm">         <div onclick="javascript:alert('new')">New</div>         <div>             <span>Open</span>             <div>    

  • 问题内容: AFAIK,Python中没有curses菜单扩展,因此您必须推出自己的解决方案。我知道这个补丁http://bugs.python.org/issue1723038,但我不知道它的当前状态是什么。我在http://www.promisc.org/blog/?p=33上找到了一个很好的Python类,用于包装我想要的“ cmenu”,但我对此也有疑问。我想制作一个菜单,用户可以选择一个

  • 本文向大家介绍js遍历map javaScript遍历map的简单实现,包括了js遍历map javaScript遍历map的简单实现的使用技巧和注意事项,需要的朋友参考一下 js遍历map javaScript遍历map的简单实现 这样会把map给遍历掉,显示在浏览器上的控制器里。 以上这篇js遍历map javaScript遍历map的简单实现就是小编分享给大家的全部内容了,希望能给大家一个参

  • 问题内容: 如果我想在python中创建一个简单的XML文件,该怎么办?(明智的) 我想要的xml看起来像: 问题答案: 如今,最流行(且非常简单)的选项是ElementTree API,该元素自Python 2.5起已包含在标准库中。 可用的选项有: ElementTree(ElementTree的基本,纯Python实现。自2.5以来是标准库的一部分) cElementTree(Element

  • 问题内容: 在Python 3中遍历一个对象时,将单个对象获取为: 如何获取1个长度的对象呢? 以下是可能的,但对读者来说不是很明显,并且很可能表现不佳: 问题答案: 如果您担心此代码的性能,并且a字节不适合您的情况,那么您可能应该重新考虑使用的数据结构,例如使用对象代替。 您可以对对象进行切片以获得1个长度的对象: 有PEP 0467-针对二进制序列的次要API改进 ,提出了以下方法:

  • 本文向大家介绍jQuery EasyUI 菜单与按钮之创建简单的菜单和链接按钮,包括了jQuery EasyUI 菜单与按钮之创建简单的菜单和链接按钮的使用技巧和注意事项,需要的朋友参考一下 菜单(Menu)定义在一些 DIV 标记中,如下所示: 当菜单创建之后是不显示的,调用 'show' 方法显示它或者调用 'hide' 方法隐藏它: 创建链接按钮(Link Button) 通常情况下,使用

  • 本文向大家介绍qml 创建一个简单的按钮,包括了qml 创建一个简单的按钮的使用技巧和注意事项,需要的朋友参考一下 示例 您可以使用MouseArea组件轻松地在可单击按钮中转换每个组件。下面的代码显示一个360x360窗口,中间带有一个按钮和一个文本。按下按钮将更改文本: