Josh C
Josh C

Reputation: 163

Using 2 dictionaries in python

I'm trying to create a code that if the user types his first name and his middle name initial into the input it will add the two dictionary numbers together.

For the first name values I want:

    d['a'] = 0
    d['b'] = 60
    d['c'] = 100
    d['d'] = 160
    d['e'] = 200
    d['f'] = 240
    d['g'] = 280
    d['h'] = 320
    d['i'] = 400
    d['j'] = 420
    d['k'] = 500
    d['l'] = 520
    d['m'] = 540
    d['n'] = 620
    d['o'] = 640
    d['p'] = 660
    d['q'] = 700
    d['r'] = 720
    d['s'] = 780
    d['t'] = 800
    d['u'] = 840
    d['v'] = 860
    d['w'] = 880
    d['x'] = 940
    d['y'] = 960
    d['z'] = 980

Middle name initial values I want:

    d['a'] = 1
    d['b'] = 2
    d['c'] = 3
    d['d'] = 4
    d['e'] = 5
    d['f'] = 6
    d['g'] = 7
    d['h'] = 8
    d['i'] = 9
    d['j'] = 10
    d['k'] = 11
    d['l'] = 12
    d['m'] = 13
    d['n'] = 14
    d['o'] = 14
    d['p'] = 15
    d['q'] = 15
    d['r'] = 16
    d['s'] = 17
    d['t'] = 18
    d['u'] = 18
    d['v'] = 18
    d['w'] = 19
    d['x'] = 19
    d['y'] = 19
    d['z'] = 19

Code so far:

first_name = raw_input("what is your first name?: ")
middle_initial = raw_input("What is your middle initial?: ")

    #First Name Initial Values
    d = {}
    d['a'] = 0
    d['b'] = 60
    d['c'] = 100
    d['d'] = 160
    d['e'] = 200
    d['f'] = 240
    d['g'] = 280
    d['h'] = 320
    d['i'] = 400
    d['j'] = 420
    d['k'] = 500
    d['l'] = 520
    d['m'] = 540
    d['n'] = 620
    d['o'] = 640
    d['p'] = 660
    d['q'] = 700
    d['r'] = 720
    d['s'] = 780
    d['t'] = 800
    d['u'] = 840
    d['v'] = 860
    d['w'] = 880
    d['x'] = 940
    d['y'] = 960
    d['z'] = 980

    lower = first_name.lower()
    first_initial = lower[0]

    if first_initial in d:
        print d[first_initial]

Example:

For example if I type Josh for the first name and J for the middle name initial the output should be 430.

If you are confused how I got 430, I added 420 and 10 together because 420 was the value of 'j' for the fist initial of the first name 'josh' and 10 is the value of 'j' for the middle name initial.

Upvotes: 0

Views: 70

Answers (3)

George Zachariadis
George Zachariadis

Reputation: 58

d = {}
d['a'] = 0
d['b'] = 60
d['c'] = 100
d['d'] = 160
d['e'] = 200
d['f'] = 240
d['g'] = 280
d['h'] = 320
d['i'] = 400
d['j'] = 420
d['k'] = 500
d['l'] = 520
d['m'] = 540
d['n'] = 620
d['o'] = 640
d['p'] = 660
d['q'] = 700
d['r'] = 720
d['s'] = 780
d['t'] = 800
d['u'] = 840
d['v'] = 860
d['w'] = 880
d['x'] = 940
d['y'] = 960
d['z'] = 980

dict = {}
dict['a'] = 1
dict['b'] = 2
dict['c'] = 3
dict['d'] = 4
dict['e'] = 5
dict['f'] = 6
dict['g'] = 7
dict['h'] = 8
dict['i'] = 9
dict['j'] = 10
dict['k'] = 11
dict['l'] = 12
dict['m'] = 13
dict['n'] = 14
dict['o'] = 14
dict['p'] = 15 
dict['q'] = 15
dict['r'] = 16
dict['s'] = 17
dict['t'] = 18
dict['u'] = 18
dict['v'] = 18
dict['w'] = 19
dict['x'] = 19
dict['y'] = 19

first_name = raw_input("what is your first name inital?: ")
middle_initial = raw_input("What is your middle initial?: ")

if first_name in dict.keys():
  value_to_add  = dict.get(first_name)

if middle_initial in d.keys():
 next_value_to_add = d.get(middle_initial)

print value_to_add,next_value_to_add
values_added = value_to_add + next_value_to_add
print values_added

That's what you are looking for I think...just you can't have 2 dictionaries with the same name and you can check and change stuff ...and you used arrays ( [] = array ) ( {} = dictionary )

Happy to help !

Upvotes: 1

Berci
Berci

Reputation: 594

I'd suggest to use (dict of) zip for two lists:

from string import lowercase as abc

d1 = dict( zip(abc, [0, 60, 100, 160, ...]) )
d2 = dict( zip(abc, [1,2,3,4,5,...]) )

...

Upvotes: 2

PradeepK
PradeepK

Reputation: 2306

First make 2 dictionaries for first name score and middle name score,

firstNameScore = {'a':0, 'b':60, 'c':100, and so forth}
middleNameScore = {'a':1, 'b':2, 'c':3, and so forth}
first_name = raw_input("what is your first name?: ")
middle_initial = raw_input("What is your middle initial?: ")
# Access the value of initials from dictionaries.
score = firstNameScore[first_name[0]] + middleNameScore[middle_initial]
print score

Upvotes: 1

Related Questions