程序运行中的While循环

2024-06-28 11:02:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序,我正在写,我希望它退出。在

意思是,当我输入Q,或者给它一个Q的输入,它应该会显示给我。。在

Press Q to quit: Q   
And it should show >>> in next line

但到目前为止我有:

^{pr2}$

现在它应该继续循环。。因为在we输入提示之后,它应该转到prompt\u riding,但是它没有…:(

>>>Select a voting system or Q to quit:
 Approval, Range, Plurality, IRV, Borda, Q
Approval
Running for Approval
Which riding would you like to see results for? (Enter a number between 0 and 307, or all.):0
Which riding would you like to see results for? (Enter a number between 0 and 307, or all.):

It should show me.. 
Select a voting system or Q to quit:
  Approval, Range, Plurality, IRV, Borda, Q

有没有可能关闭无限while循环?在


Tags: ortoforshowrangeselectsystemquit
2条回答

我我是你我会用tkinter操纵事件:

# respond to a key without the need to press enter
import Tkinter as tk
def keypress(event):
    if event.keysym == 'Q':
        root.destroy()
    x = event.char

root = tk.Tk()
print "Press Q to Quit:"
print ">>> "
root.bind_all('<Key>', keypress)
# don't show the tk window
root.withdraw()
root.mainloop()

你可以试试这个:

input_prompt = '' # To get into the loop first time

# Start loop
while input_prompt != 'Q':

    # Now ask for voting system
    input_prompt = prompt_from_list('Select a voting system or Q to quit:', list)
        ...

    if input_prompt == 'Approval':
        ...
        prompt_riding = input("Which riding would you like to see results for? (Enter a number between 0 and 307, or all.):")

        # If the user has selected to Quit
        if prompt_riding == 'Q':
            break # Exit loop

        # Continue checking what prompt_input is
        ...

相关问题 更多 >