0

Say I have to get user inputs for # of rows and columns, along with asking how they would want their columns justified. Say for this example they want it right justified. How would I go about this?

numofrows = rawinput('# rows:')
numofcolumns = rawinput('# columns:')
for x in range(numberofcolumns):
    justify = rawinput('Do you want left, center, or right?')

1 Answer 1

1

It appears that you are asking how to request justification settings for the separate columns, which are numofcolumns in number. The simplest changes to your program-so-far are to correct rawinput to raw_input, and add a column number to the last prompt, and save results in a list:

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

For example, with ncolumns = 4, four questions like

Select left, center, or right justification in column 1 by L,C,R:

will appear, and if users responds with letters L, R, C, and L in turn, in upper or lowercase, the print statement will print ['L', 'R', 'C', 'L'].

Of course such an interface is clumsy to use; on the one hand, if the user notices a mistake in column 4's specification after entering a dozen other specifications, it is necessary to start all over. On the other hand, the program is difficult to use from a script. The example code shown below does not address the latter problem, but does make it easy for the user to make column-justification settings in any order. This code displays a message, Select column justification settings, then click Go and puts up a grid of radio buttons labeled L, C, R. After the user is satisfied with justification settings and clicks Go, the program prints a list like [1, 1, 1, 0, 2], which indicates C,C,C,L,R selections as shown in following figure. grid of radio buttons This demo program does not input nrows or ncolumns, but sets 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.