School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,149 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,715 people online right now. Registration is fast and FREE... Join Now!




Using Tuples or list with the raw_input command

 

Using Tuples or list with the raw_input command

jaziey

12 Oct, 2009 - 10:57 PM
Post #1

New D.I.C Head
*

Joined: 23 Sep, 2009
Posts: 6

I am trying to create a python program that asks the number of motorcycles sold during each month in a year. I have created a function that takes in the number of bikes for each month, creating an integer list with 12 values. But the program asks 12 times, "What is the number of bikes sold for Jan", "What is ...Feb".. and so on. I tried to create a tuple that puts in the name of the month as a loop. But I am having no success.

CODE

for i in range(TWELVE):
   bike[i]=int(raw_input("Enter the bikes for")) monDate[i]


I know this won't work raw_input is expecting an integer only. Any ideas?

User is offlineProfile CardPM
+Quote Post


girasquid

RE: Using Tuples Or List With The Raw_input Command

13 Oct, 2009 - 09:01 AM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,648



Thanked: 58 times
Dream Kudos: 825
My Contributions
Use the calendar.month_name list to work with month names:
CODE

>>> import calendar
>>> for i, name in enumerate(calendar.month_name):
...  bike[i] = int(raw_input("Enter the bikes for %s" % name))

According to the documentation on month_name, there's an empty element in the list at the start - so you may want to first filter calendar.month_name through something like bool in order to keep yourself from working with an empty value. Here's another way of doing it that will store the values into your array, along with prompting for names correctly:
CODE

>>> import calendar
>>> for name in filter(bool, calendar.month_name):
...  bike.append(int(raw_input("Enter the bikes for %s: " % name)))


Hope that helps.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:15PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month