views:

92

answers:

2

So, i have recently started learning python...i am writing a small script that pulls information from a csv and i need to be able to notify a user of an incorrect input

for example

the user is asked for his id number, the id number is anything from r1 to r5 i would like my script to be able to tell the user that they have input something wrong for example, if the user inputs a1 or r50, the user needs to be notified that they have input the wrong parameters. how do i do this?

i have looked into def statements, but i cannot seem to grasp all the syntax in python....(i dont know all the commands...parameters and stuff)

any help would be very much appreciated =D

while True: 
    import csv 
    DATE, ROOM, COURSE, STAGE = range (4) 
    csv_in = open("roombookings.csv", "rb") 
    reader = csv.reader (csv_in) 
    data = [] 
    for row in reader: 
        data.append(row) 
    roomlist = raw_input ("Enter the room number: ") 
    print "The room you have specified has the following courses running: " 
    for sub_list in data: 
        if sub_list[ROOM] == roomlist: 
            Date, Room, Course, Stage = sub_list 
            print Date, Course
+1  A: 

I'm not sure what are you asking for, but if you wish to check if user entered correct id, you should try regular expressions. Look at Python Documentation on module re. Or ask google for "python re"

Here's an example that will check user's input:

import re

id_patt = re.compile(r'^r[1-5]$')
def checkId(id):
    if id_patt.match(id):
        return True
    return False

HTH, regards.

EDIT: I read you're question again, here's some more code: (just paste it below previous code fragment)

validId = False
while not validId:
    id = raw_input("Enter id: ")
    validId = checkId(id)

By the way, it could be written in quite shorter way, but this piece of code should be easier to understand for someone new to Python.

paffnucy
thanks for all the help! =D these answers have helped me alot!, i think im finally getting a grip on this script.. im horrible at programming
+1  A: 

Seriously, read a tutorial. The official one is pretty good. I also like this book for beginners.

import csv

while True:
    id_number = raw_input('(enter to quit) ID number:')

    if not id_number:
        break

    # open the csv file
    csvfile = csv.reader(open('file.csv'))
    for row in csvfile:
        # for this simple example I assume that the first column 
        # on the csv is the ID:
        if row[0] == id_number:
            print "Found. Here's the data:", row
            break
    else:
        print "ID not found, try again!"


EDIT Now that you've added code, I update the example:

import csv
DATE, ROOM, COURSE, STAGE = range(4) 

while True: 
    csv_in = open("roombookings.csv", "rb") 
    reader = csv.reader(csv_in) 
    roomlist = raw_input("(Enter to quit) Room number: ") 
    if not roomlist:
        break
    print "The room you have specified has the following courses running: " 
    for sub_list in reader: 
        if sub_list[ROOM] == roomlist: 
            print sub_list[DATE], sub_list[COURSE]
nosklo
thanks for all the help! =D these answers have helped me alot!, i think im finally getting a grip on this script.. im horrible at programming