[Tutor] input problem ?

alan.gauld@bt.com alan.gauld@bt.com
Thu, 6 Sep 2001 23:12:02 +0100


Walter,

Others have dealt with the errors you got, here are a 
couple of other things to consider.

> vraag_234 = input ( "Wat is de score bij vraag 234? ")

Use raw_input() rather than input() - it's safer should 
you get a knowledgable and rogue user. You need to convert 
the raw_input to an int thus:

vraag_317-321 = int(raw_input ("Wat is de score bij vraag 317-321?"))

Also to save code and make the program easier to extend you could
store the questions and responses in a dictionary:

vraag = {'317-321':["Wat is de score bij vraag 317-321?", 4, None],
         '234':["Wat is de score bij vraag 234? ", 4, None],
         etc...
}

Here we use the question number as a key and store the question,
the compared value(weight) and a place marker(None) for the 
input result. 
Thus to access the question we can do:
vraag['234'][0]
To access the weighting value:
vraag['234'][1]
and to store a value:
vraag['234'][2] =  42

To ask the questions you can then just loop over the keys:

for q in vraag.keys():
    # vraag[q] is the dictionary item, 
    # 2 is the value field and 0 the question text
    vraag[q][2] = int(raw_input(vraag[q][0]))

The advantage here is that you can add new questions to the 
dictionary or change the weighting values without changing the
actual code structure.

Combining that with the earlier tip about using a count value 
you can then later do:

for q in vraag.keys():
    # compare value and weighting
    if vraag[q][2] > vraag[q][1]:
       count = count + 1

> if (vraag_234 > 4) or (vraag_326 > 4):

This doesn't change much:

if (vraag['234'][2] > vraag['234'][1]) 
    or (vraag['326'][2] > vraag['326'][1]):
>     print "Er kan sprake zijn van een depressie ..."

>     Is there to much input?, should i make functions?

You could do it in functions too but if you use a dictionary 
the code shrinks to the point where its not so important.
Structuring the data to suit the problem can often save 
an awful lot of repetitive coding.
 
OTOH you might find the complexity of using indexes too 
difficult to track, in which case a function might be better.

Finally, in your original code you used something like this:

List = []
if (vraag_317-321 > 4):
    List.append

That won't do anything to the list since List.append is 
not being executed - its simply the name of a function. 
You need to add parentheses - and put something inside them!

eg. List.append(1)

> Maybe  a difficult try out but to me it is very usefull if it works

For a first attempt its very good, my first programs 
in a new language are usually much less ambitious! :-)

Alan G