1

I'm really new to programming so I wasn't exactly sure how even to word my question. What I am trying to accomplish is to allow a user to enter in attributes about a specific item over several items and record each value to a variable.

So for instance, cars. User would be prompted with three questions about the car: Make, model, year. This process would loop until there are no items left.

This is what I've got:

while True:
    answer=raw_input('Is there another car? Y/N')
    if answer=Y:
        make=raw_input('Car manufacturer?')
        model=raw_input('Car Model?')
        year=raw_input('Year?')
    elif answer=N:
        break
    else:
        print 'Incorrect Response'

I know the code is really shakey, but the goal is that every time the loop passes, it records the user inputs to a new set of variables (for instance, make1, model1, year1, make2, model2, etc). That way I can compile all the data afterwards without the variables being over-written with each pass, like it would with my current code.

Thanks for your help.

2
  • 2
    Rather than having a series of variables make1, make2, make3, consider making a single list variable makes, which contains multiple values. Commented Jun 16, 2014 at 20:49
  • What a about a list of lists, where each element is a list of the tipe [make, model, year] ? Commented Jun 16, 2014 at 20:53

2 Answers 2

1

Why not accumulate a tuple of values in a list? It's similar to building a table of results, and each row in the table corresponds to your tuple.

Try this:

results = []

while True:
    answer=raw_input('Is there another car? Y/N')
    if answer == 'Y':
        make = raw_input('Car manufacturer?')
        model = raw_input('Car Model?')
        year = raw_input('Year?')
        results.append((make, model, year))
    elif answer == 'N':
        break
    else:
        print 'Incorrect Response'
for result in results:
    print result

and you'll print

(make1, model1, year1)
(make2, model2, year2)
... and so on

You can get fancier with a named tuple:

import collections
Car = collections.namedtuple('Car', 'make model year')

results = []

while True:
    answer=raw_input('Is there another car? Y/N')
    if answer == 'Y':
        make = raw_input('Car manufacturer?')
        model = raw_input('Car Model?')
        year = raw_input('Year?')
        results.append(Car(make, model, year))
    elif answer == 'N':
        break
    else:
        print 'Incorrect Response'
for car in results:
    print car.make, car.model, car.year

A named tuple is a tuple with a namespace like an object, but not as heavy on your Python processes' memory. A full object stores attributes in a dict, which is much more memory heavy.

Sign up to request clarification or add additional context in comments.

1 Comment

if answer=Y should be if answer=='Y'; the same for answer=N. Don't forget that 'Y' and 'N' are string, otherwise they are not defined in your code.
1

Use a class which you could consider naming Car:

class Car:
     pass

Then, you can instantiate an empty list of cars,

cars = []

and, during the while-loop, have a new car initialized and appended to your list:

car = Car()   
car.make=raw_input('Car manufacturer?')
car.model=raw_input('Car Model?')
car.year=raw_input('Year?')
cars.append(car)

Classes represent persistent objects. All elements are kept 'alive' in the list, and you can, after the user input is finished, summarize the input, or whatever you want to do. Read the python 2.7 manual about lists and classes to learn more.

3 Comments

Thanks for the advice! Didn't think I'd get responses so quickly. Much appreciated.
A list of objects with attributes is heavier weight, and this is good for learning on, but I wouldn't put this into production.
Yes, I saw your post on named tuples, which I didn't know about so far. Yet, considering the application in question, I wanted to a) point out basic concepts and b) do not think that memory will be an issue here :)

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.