0

I am using Python 2.7 and Gedit. I wrote a simple program to calculate my income. Now I think it would be useful to house my variables in an array, is this possible?

# this is a test program

# my work week that I wish could hold the values of the following variables
workweek = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday',        'saturday']

# The variables I would like to place in an array
sunday = 0
monday = 0
tuesday = 0
wednesday = 9
thursday = 9
friday = 9
saturday = 9

print sunday + monday + tuesday + wednesday + thursday + friday + saturday

# my wage for sunday through thursday
weekdaywage = 4.25

# my wage for friday and saturday
weekendwage = 3.25

# this is my wages on thursday...
print thursday * weekdaywage

# this is coming out as an error? What can I do as a workaround
# I did a googlesearch
# All I need is a link to learning material if you don't have time to explain
print workweek * weekdaywage

1 Answer 1

2

I think you need a dictionary?

workweek = {
    'sunday': 0
    'monday': 0
    'tuesday': 0
    'wednesday': 9
    'thursday': 9
    'friday': 9
    'saturday': 9   
}

You can retrieve the value of a variable using:

workweek['monday']

This does mess up the order, if you wish to maintain the order of the days, use an OrderedDict

from collections import OrderedDict
p = OrderedDict([('monday', 1), ('tuesday', 0)])
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.