당신이 수의 numofcolumns
있는 별도의 컬럼에 대한 정당성 설정을 요청하는 방법을 묻는 것으로 보인다. 네 가지 질문처럼, ncolumns = 4
으로, 예를 들어
nrows = int(raw_input('# rows: '))
ncolumns = int(raw_input('# columns: '))
justify = []
for x in range(ncolumns):
j = raw_input('Select left, center, or right justification in column '+str(x+1)+' by L,C,R: ')
justify.append(j.upper())
print justify
: 당신에 대한 간단한 변경 프로그램은 그리 멀리 raw_input
에 rawinput
를 수정, 마지막 프롬프트에 열 번호를 추가하고 목록에서 결과를 저장할 수 있습니다
Select left, center, or right justification in column 1 by L,C,R:
이 표시, 사용자 경우 인쇄 문 ['L', 'R', 'C', 'L']
를 인쇄합니다, 대문자 또는 소문자로, 다시 문자 L, R, C, 및 L로 응답합니다.
물론 이러한 인터페이스는 사용하기에 불편합니다. 한편으로 사용자가 12 개의 다른 명세를 입력 한 후 4 열의 스펙에서 실수를 발견하면 모든 것을 다시 시작해야합니다. 반면에 프로그램은 스크립트에서 사용하기가 어렵습니다. 아래에 표시된 예제 코드는 후자의 문제를 해결하지 않지만 사용자가 임의의 순서로 열 맞춰 정렬 설정을 쉽게 작성합니다. 이 코드는 Select column justification settings, then click Go
이라는 메시지를 표시하고 L, C, R이라는 라디오 버튼 격자를 표시합니다. 사용자가 정렬 설정을 만족하고 Go를 클릭하면 프로그램은 [1, 1, 1, 0, 2 ]는 다음 그림과 같이 C, C, C, L, R 선택을 나타냅니다. 이 데모 프로그램은 nrows
또는 ncolumns
을 입력하지 않지만 ncolumns=5
을 설정합니다.
#!/usr/bin/env python
# Set up a simple horizontal menu with some editable radio buttons
import gtk
def bcallback(w, i, j): # Radio button callback
settings[i] = j
# create set of radio buttons
ncolumns = 5
W = gtk.Window()
W.connect('destroy', gtk.main_quit)
W.set_title('Column-justification settings')
box = gtk.HBox()
W.add(box)
for i in range(ncolumns):
col = gtk.VBox()
b = gtk.RadioButton(None, 'L')
rb = [b, gtk.RadioButton(b, 'C'), gtk.RadioButton(b, 'R')]
for j,b in enumerate(rb):
col.pack_start(b)
b.connect('toggled', bcallback, i, j)
box.pack_start(col)
b = gtk.Button(None, 'Go')
b.connect('button-press-event', gtk.main_quit)
box.pack_start(b)
W.show_all() # Make menu available for showing
settings = [0 for i in range(ncolumns)]
print 'Select column justification settings, then click Go'
gtk.main()
print settings