GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Oct-2006, 12:57
danielfolsom danielfolsom is offline
New Member
 
Join Date: Oct 2006
Posts: 2
danielfolsom is on a distinguished road

Printing


Ok, I'm trying to do a code just to get used to OOP, i'm kinda a newbie, but ... here's the code:

Python Code:
class Ipod:
    def _init_ (self, type, color, space):
        self.color = color
        self.space = space
        self.type = type

    def show(self):
        print self.color + correspond[self.space] + self.type

correspond = {'2GB': 500, '4GB': 1000, '8GB': 2000, '30GB': 7500, '80GB':20000}

def Ask():
    IpodNano = Ipod()
    IpodNano.color = raw_input ("Silver, Pink, Black, Green, Blue")
    IpodNano.space = raw_input ("2GB 4GB or 8GB")
    IpodNano.type = "Ipod Nano"
    IpodNano.show()

    IpodRegular = Ipod()
    IpodRegular.color = raw_input ("Black, White")
    IpodRegular.space = raw_input (" 30GB, 80GB ")
    IpodRegular.type = "Ipod Regular"
    IpodRegular.show()


question = input ("How many people?")
for i in range(question):
    Ask()

I know that the problem is where I say print correspond[self.space], but I DON'T know how to get it to work, any suggestions? :-D
Last edited by admin : 02-Oct-2006 at 03:40. Reason: Please insert your PYTHON code between [py] & [/py] tags
  #2  
Old 02-Oct-2006, 00:26
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,635
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Printing


Could you repost again using the python code tags ([py][/py]) around your code? I can't tell how your indents are supposed to be, especially for the correspond dictionary.

Offhand, type is a Python keyword, so you might want to change the name for that variable.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #3  
Old 02-Oct-2006, 03:47
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 858
admin will become famous soon enough

Re: Printing


Quote:
Originally Posted by crystalattice
...

Offhand, type is a Python keyword, ...

It is? I used this list, for the syntax highlighting here. It doesn't include "type".
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #4  
Old 03-Oct-2006, 22:12
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,635
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Printing


When I tried writing the above program in IDLE, the default Python editor (which isn't the one I normally use but I was lazy), type came up color coded. I've never used it as a variable name by itself, so I didn't think anything about it. I was simply going on what IDLE gave me.

I double-checked it in SPE and it didn't color code it. Could just be a fluke in the IDLE markup code. Sorry about the confusion.

So danielfolsom, the answer to your problem is in the error code the Python interpreter spits out when you run the code.
Quote:
Traceback (most recent call last):
File "Desktop/ipod.py", line 28, in ?
Ask()
File "Desktop/ipod.py", line 17, in Ask
IpodNano.show()
File "Desktop/ipod.py", line 8, in show
print self.color + correspond[self.space] + self.type
TypeError: cannot concatenate 'str' and 'int' objects
Your print line is trying to put a string value together with an integer value, which it can't do. Try casting your integer into a string first then print it.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #5  
Old 09-Oct-2006, 10:32
Ene Uran Ene Uran is offline
New Member
 
Join Date: Oct 2006
Location: USA West
Posts: 8
Ene Uran is on a distinguished road
Exclamation

Re: Printing


In Python type is not a keyword but a builtin function. Don't use it as a variable name or the following will happen:
Python Code:
>> type = 3
>>> type(type)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'int' object is not callable
>>> 
Same goes for int, list, str, float and so on!
This will give you a list of all builtins:
Python Code:
print '\n'.join(sorted(dir(__builtins__), key=str.lower))
  #6  
Old 09-Oct-2006, 13:29
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,635
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Printing


So that would explain the disparity between the IDEs; IDLE, being the default IDE created by Guido himself, will show you the reserved words. SPE and others probably won't show you these words.

Good to know.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Printing posters... blumagic Graphics Forum 0 03-Apr-2006 08:11
C++ printing contents of txt files etc Venom C++ Forum 3 18-Mar-2005 22:12
Printing problem Frandy MS Visual C++ / MFC Forum 1 16-Feb-2005 13:47
printing nos in vc++ kumaran MS Visual C++ / MFC Forum 3 09-Jan-2005 23:28
I cant believe this doesnt work: - basic printing of integers to screen davo C Programming Language 2 24-Jul-2004 21:50

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:46.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.