0

Python 2.7 と Gedit を使用しています。収入を計算する簡単なプログラムを書きました。変数を配列に格納すると便利だと思いますが、これは可能ですか?

# 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
4

1 に答える 1

2

辞書が必要だと思いますか?

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

次を使用して変数の値を取得できます。

workweek['monday']

これは順序を台無しにします。日の順序を維持したい場合は、OrderedDictを使用してください

from collections import OrderedDict
p = OrderedDict([('monday', 1), ('tuesday', 0)])
于 2012-06-01T11:50:44.350 に答える