-1

私が書いているこのプログラムがあり、それを終了させたいと思っています。

つまり、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 ループを閉じる方法はありますか?

4

2 に答える 2

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
        ...
于 2012-11-30T08:35:07.323 に答える
0

II なら、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()
于 2012-11-30T08:39:37.647 に答える