gpt4 book ai didi

Python 诅咒 : Returning to the Previous Menu

转载 作者:太空宇宙 更新时间:2023-11-04 06:27:32 27 4
gpt4 key购买 nike

我在 ncurses 中有一个菜单系统。选择其中一个选项会将您带到另一个菜单。但我该如何返回?

import curses

def Main():
x = 0
while x!= ord('2'):
x = screen.getch()
screen.clear();screen.border();
screen.addstr(1,1, "Please choose:")
screen.addstr(3,1, "1 - Another Menu")
screen.addstr(4,1, "2 - Exit")

if x==ord('1'):
y = 0
while y!= ord('2'):
y = screen.getch()
screen.clear();screen.border();
screen.addstr(1,1, "Please choose from new menu:")
screen.addstr(3,1, "1 - Do Something new")
screen.addstr(4,1, "2 - Previous Menu")
if y == ord('1'): doSomething()

#Here I exit the internal loop. I need to go back to the previous menu, but I don't know how.
##
##exit outside loop and close program
##
curses.endwin(); exit();

screen = curses.initscr()
Main()

理想情况下,我需要使用 GOTO 模块在代码行之间跳转,但我使用的设备没有内置该模块。

你们知道其他方法吗?非常感谢任何帮助。

============更新:==================

好的,我还意识到您可以轻松地重新生成两个菜单:

import curses

def Main():
x = 0
while x!= ord('2'): #draws 1st menu
screen.clear();screen.border();
screen.addstr(1,1, "Please choose:")
screen.addstr(3,1, "1 - Another Menu")
screen.addstr(4,1, "2 - Exit")
x = screen.getch() #grab input AFTER first giving options :)
if x==ord('1'):
y = 0
z = 0
while y!= ord('2'): #draws 2nd menu
screen.clear();screen.border();
screen.addstr(1,1, "Please choose from new menu:")
screen.addstr(3,1, "1 - Do Something new")
screen.addstr(4,1, "2 - Previous Menu")
screen.addstr(6,1, "current loop : "+str(z))
y = screen.getch(); #grabs new input
while z!= -1: #never breaks from loop unless 'break' is called
if y == ord('1'):
z += 1
break #regenerates 2nd menu
break #regenerates 1st menu

#Here we exit the internal loop.
##
##exit outside loop and close program
curses.endwin(); exit();

screen = curses.initscr()
Main()

最佳答案

在第二个 while 循环结束后添加 x = 0

(您需要在循环中每次重置x,而不仅仅是第一次。否则从第一个菜单退出将x设置“退出”,因此也将退出第二个菜单。)

关于Python 诅咒 : Returning to the Previous Menu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6635633/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com