2012-11-30 47 views
-1

我有这个程序,我正在写,我希望它退出。while循环在程序运行

含义,当我在Q型,或给它的Q输入,它应该显示我..

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

但到目前为止,我有:

list = ['Approval', 'Range', 'Plurality', 'IRV', 'Borda', 'Q'] 
input_prompt = prompt_from_list('Select a voting system or Q to quit:', list) 
while input_prompt != 'Q': 

approval_file = open(APPROVAL_BALLOT_FILENAME, 'r') 
approval = approval_file.readlines() 
approval_file.close() 




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

    list = format_approval_list(approval, prompt_riding) 
    a = vs.voting_approval(list) 

    country = print_country_results(a[1]) 
elif input_prompt == 'Range': 
    print('Running for Range') 
    prompt_riding = prompt_for_riding("Which riding would you like to see results for? (Enter a number between 0 and 307, or all.):", 
         307) 
     print prompt_riding 

现在应该继续循环..为我们输入提示后,它应该去prompt_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循环?

+1

你为什么让每行后结肠? –

+0

所以它会显示在下一行..我是新的堆栈溢出,所以不太熟悉的格式对不起 – user1853961

+1

@ user1853961 ..格式化您的代码。粘贴在这里。选择完整的代码,并按Ctrl + K来添加代码标签。 –

回答

1

你可以试试这个:

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 
     ... 
+0

什么是我的raw_input?并且我不使用无限循环 – user1853961

+0

@ user1853961'raw_input()'返回您输入的任何内容 - 阅读[documentation](http://docs.python.org/2/library/functions.html#raw_input)或尝试它在你的翻译,看看它是什么:) –

+0

这种方法的作品,除了它在某些时候停止循环..我不知道如何指定 – user1853961

0

我我是你,我会用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()