2 Replies - 1552 Views - Last Post: 06 February 2009 - 09:21 AM Rate Topic: -----

#1 dustamust  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-January 09

Beginner Python Help

Posted 30 January 2009 - 06:47 PM

Hello new to python and need help with a project i'm trying to figure out. I'm trying to ask the user
to enter a month and the output should show 'Months' birthstone is "". I am trying to figure it out using lists and cant figure out how to convert one list to another.
Here's the code I have so far:

months = ['January','February','March','April','May','June','July','August','September','October','November','December']

birthStones = ['Garnet','Amethyst','Aquamarine','Diamond','Emerald','Pearl','Ruby','Peridot','Sapphire','Opal','Topaz','Turquoise']

question = raw_input ('Please enter a Month : ')

question2 = question.capitalize()

firstIndex = months.index(question2)

output = (firstIndex + "'s Birth Stone is ")

print output

Is This A Good Question/Topic? 0
  • +

Replies To: Beginner Python Help

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,635
  • Joined: 16-October 07

Re: Beginner Python Help

Posted 30 January 2009 - 08:10 PM

Maybe something like:
print question + "'s Birth Stone is " + birthStones[firstIndex]


Was This Post Helpful? 0
  • +
  • -

#3 KurtB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-February 09

Re: Beginner Python Help

Posted 06 February 2009 - 09:21 AM

You could try this:....

#!/usr/bin/env python
# encoding: utf-8
"""
birthstone.py

usage: birthstone.py month
"""

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
	'August', 'September', 'October', 'November', 'December']
birthStones = ['Garnet', 'Amethyst', 'Aquamarine', 'Diamond', 'Emerald',
	'Pearl', 'Ruby', 'Peridot', 'Sapphire', 'Opal', 'Topaz', 'Turquoise']

# mash the two lists into a list of tuples...
foo = zip(months,  birthStones)
# then turn that list into a dictionary....
data = dict(foo)

question = raw_input ('Please enter a Month :  ')

# I took the liberty of changing some variable names for readability. 
# Of course, you could make it more efficient by just redefining 'question',
# but it makes more sense to me this way.

month = question.capitalize()
stone = data.get(month)

# You can just just straight to print, and the substitution below 
# is more pythonic, I've been told. 
print "%s's Birth Stone is %s." % (month, stone)




And then later put in a try except to catch typos.

Hope this helps.

This post has been edited by KurtB: 06 February 2009 - 09:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1