1

I have a list of names that are all different e.g. carpentry, engineering 15 in total. In the piece of code below I have called it name1, however there will be no sequence to the variable names. Instead of writing this code out 15 times how can I create a function that executes the same conditions for each one while changing the name each time.

name1= int(raw_input("Please enter your value for name1: "))
if name1 <5:
status = "nill"
total = 0
if name1 >4 and name1 <8:
status = "valid"
total=1
if name1 >7 and name1 <12:
status = "superb"
total=5
if name1 >11:
status = "over qualified"
total=10
print "this is your current status ",status 
print "this equals ",total
2
  • Don't change the name each time. Use a dictionary or a list instead. Commented Dec 29, 2012 at 23:23
  • @miguel Please use proper formatting for your code (indentation). It is Python, indentations matter ... Commented Dec 29, 2012 at 23:27

1 Answer 1

1

I believe this does what you are looking for. Note this is likely not the way that you want to store the individual statuses (a defaultdict would likely make more sense), but hopefully this makes sense from a conceptual standpoint:

def comparisons(value):
    """Only change here is using x <= y < z syntax."""
    if value < 5:
        status = 'nill'
        total = 0
    elif 5 <= value < 8:
        status = 'valid'
        total = 1
    elif 8 <= value < 12:
        status = 'superb'
        total = 5
    else:
        status = 'over-qualified'
        total = 10
    # Here we return the status and the total for each item
    # This may not be what you want, so this can be adjusted
    return status, total


# Create a list that will contain your 15 items
items = ['Engineering', 'Carpentry']

# Create a container to hold the results.
# Note that we don't use different variables names each time -
# instead, we create an entry in a dictionary corresponding to
# our 'items' values.
results = {}

# Now we iterate through each item in our items list, running
# our function and storing the results. Note that this is a guess
# as to how you want to handle 'status' - a more useful solution
# could be to use a nested dictionary where each item has a dictionary
# with two sub-fields: 'status' and 'total'.
for item in items:
    status, total = comparisons(int(raw_input('{0}: '.format(item))))
    results[item] = [status, total]

# Print out each item
print '\nIndividual items:'
for item, values in results.iteritems():
    print item, values

# Example of how you could sum the totals
print '\nTotal score:'
print sum((results[x][1] for x in results))

Output:

Engineering: 10
Carpentry: 5

Individual items:
Engineering ['superb', 5]
Carpentry ['valid', 1]

Total scores:
6
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.