A few python questions

Creating classes, methods, functions

Page 1 of 1

5 Replies - 1280 Views - Last Post: 15 August 2009 - 11:34 AM Rate Topic: -----

#1 DmS1ckwit1t  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 05-January 09

A few python questions

Post icon  Posted 13 August 2009 - 02:53 PM

Alright I have gone through several tutorials for learning the basics of python and feel I should start atempting to make some legit programs with it. I am going to start by making programs of Martyr2's Mega Project Ideas List. I was thinking about starting with the 'Find Cost of Tile to Cover W x H Floor – Calculate the total cost of tile it would take to cover a floor plan of width and height, using a cost entered by the user.' Now if im not mistaken shouldnt it be Width x Length to find the amount of tiles in a given area? And would it be smart to create my own function to do this? I have never created my own classes, functions, methods or anything before. I was taught programming in visual basic 6 which isnt the best to start with due to the fact that it puts form before function. Anywho or should I just go about coding it head since im most likely only going to need to call upon the function once if I do create. If someone could give me a tutorial or explanition to creating classes and what not that would be great. Thanks,

DmS1ckwit1t

How can I loop the question untill the user selects yes? - Nevermind figured it out :)

Heres what I have so far:

*EDIT* Would my code still work if I changed all of the While Statments To if minus the one checking for yes?

""" Tile Cost Calculator
	TileCostCalc.py
	Adapating to python
	August 13th, 2009
	Dillon Mulroy
	"""
#Sets the price of ea. tile and stores it as an integer.
tileCost = raw_input("What is the price per tile? ")
tileCost = int(tileCost)
prompt1 = "Is %d$ the correct price per tile? Y/N " %(tileCost)
#Creates a string variable for the while statments.  yn.upper() will make it easier to test in a conditional statement, as will counting the length of the string.
yn = raw_input(prompt1)
yn = yn.upper()
numChars = len(yn)
#Tests if string yn is "Y" if yes it moves on into the program
while yn == ("Y"):
	print "Okay, onto the next question..."
	length = raw_input("In feet, what is the length of the room? ")
	length = int(length)
	prompt2 = "is %dft the correct price per tile? Y/N " %(length)
	yn2 = raw_input(prompt2)
#Tests if string yn is "N" if false it loops back to setting variable tileCost
while yn == ("N"):
	tileCost = raw_input("What is the price per tile? ")
	tileCost = int(tileCost)
	yn = raw_input(prompt1)
	yn.upper()
	numChars= len(yn)
#Test if string yn is a valid answer if false it loops back to setting variable tileCost
while yn!= ("Y" , "N"):
	print "This is an invalid answer."
	print
	tileCost = raw_input("What is the price per tile? ")
	tileCost = int(tileCost)
	yn = raw_input(prompt1)
	yn.upper()
	numChars= len(yn)
#Tests if string yn is short enough to be a valid answer if false it loops back to setting variable tileCost
while numChars > 1 :
	print "There are to many characters in your answer please retry..."
	print
	tileCost = raw_input("What is the price per tile? ")
	tileCost = int(tileCost)
	yn = raw_input(prompt1)
	yn.upper()
	numChars= len(yn)
	

		
		







This post has been edited by DmS1ckwit1t: 13 August 2009 - 08:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: A few python questions

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: A few python questions

Posted 13 August 2009 - 03:25 PM

Quote

I was thinking about starting with the 'Find Cost of Tile to Cover W x H Floor – Calculate the total cost of tile it would take to cover a floor plan of width and height, using a cost entered by the user.' Now if im not mistaken shouldnt it be Width x Length to find the amount of tiles in a given area?
Sure. I think the formula was given as width x height because of thinking of the floor plan as shown on the screen. It's a giant rectangle, with one dimension running left and right on the screen (the width), and one dimension running up and down on the screen (so it looks like a height). But yes, it would be length…

Quote

And would it be smart to create my own function to do this?
I don't see a requirement to do so. It's up to you.

Quote

Anywho or should I just go about coding it head since im most likely only going to need to call upon the function once if I do create.
Go ahead and write code. Too many beginners just sit back and debate over the perfect construction for their code. It's a meaningless question really when you ask something open ended like that. Largely, the only thing we can do is give you a solution that we would write. Which isn't programming anymore. At that point, we do the programming, you just copy paste or type at the keyboard… Write something. Look back at it. Make mistakes. Learn from them.

Quote

If someone could give me a tutorial or explanition to creating classes and what not that would be great.
We could…but if we didn't what would you do? Would you Google? Would you consult various documentation sources? Would you check various forums and blog posts for examples? When faced with something you don't know, how do you learn about it? Do you really need to ask us this question?
Was This Post Helpful? 0
  • +
  • -

#3 DmS1ckwit1t  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 05-January 09

Re: A few python questions

Posted 13 August 2009 - 05:20 PM

Good smart points, and for my code would a while statement be the way to go?
I'm not on a computer with a python IDE installed on it at the moment but i was thiniking thatd be the way to go.

Quote

We could…but if we didn't what would you do? Would you Google? Would you consult various documentation sources? Would you check various forums and blog posts for examples? When faced with something you don't know, how do you learn about it? Do you really need to ask us this question?


Yes, but then what is the point of seeking advice in this forum? I'm not asking anyone to do my homework. To be able to do "homework" you msut first be taught the lesson.

This post has been edited by DmS1ckwit1t: 13 August 2009 - 06:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: A few python questions

Posted 13 August 2009 - 08:43 PM

Quote

for my code would a while statement be the way to go?
Don't know. Try it out. Does it work? Verify the program by testing it against various inputs. Check the logic and see if it makes sense.

Quote

Yes, but then what is the point of seeking advice in this forum? I'm not asking anyone to do my homework. To be able to do "homework" you msut first be taught the lesson.
Question for you: what is programming fundamentally about? I'll answer that. It's about problem solving. In particular, programmers need to be able to tackle a problem they have never seen before.

This demands a few things from the programmer. When facing an unknown problem, to push forward. To try out solutions and accept failure, learning from the failures and trying something better. To be curious and explore ideas. To break down something that looks impossible to solve into parts that can be solved. To be able to approach a problem from various angles.

Not giving you the answer is teaching you a lesson. It's why you can't read your way to competence. You need to do to learn. If I ask you "what have you tried?" and you answers don't sum to "everything possible", you didn't do enough. You gave up at some point and are trying to substitute your efforts with a guaranteed answer from us.

At some point, you will face problems we will refuse to hand out answers to. You will stare at problems without the guaranteed backing of experienced programmers. You will not know whether your approach is right or not. You won't know how to verify it's correct or not. You won't know how to isolate problems. You won't know how to learn from failures.

You might not like the fact that I do not throw out comfortable guarantees behind your ideas. But I think it's not educational to do so. You'll discover this at some point too, if you make it far enough.
Was This Post Helpful? 0
  • +
  • -

#5 DmS1ckwit1t  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 05-January 09

Re: A few python questions

Posted 14 August 2009 - 08:35 PM

Okay I figured out how to get my program to loop succesfully :) Thanks for pushing me to answer my own questions. Heres what I have so far
loopOne = 1
while loopOne:
	tileCost = raw_input("What is the price per tile? ")
	tileCost = int(tileCost)
	prompt1 = "IS $%d the correct price per tile? Y/N " % (tileCost)
	yn = raw_input(prompt1)
	yn = yn.upper()
	numChars = len(yn)#Look into a ifnumeric function
	if yn == ("Y"):
		print "Okay, onto the next question..."
		length = raw_input("In feet, what is the length of the room? ")
		length = int(length)
		prompt2 = "is %dft the correct length of the room? Y/N " %(length)
		yn2 = raw_input(prompt2)
	elif yn !=("Y"):
		tileCost = 0
		tileCost = int(tileCost)
		prompt1 = "IS $%d the correct price per tile? Y/N " % (tileCost)
		yn = ""
	else:
		print "Something went wrong"

Was This Post Helpful? 0
  • +
  • -

#6 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 202
  • Joined: 21-April 09

Re: A few python questions

Posted 15 August 2009 - 11:34 AM

Define a function:
def foo(*args, **kwargs):
Note that you can have any number of args (which come first since they are positional) or keyword args (which are optionally positional)
Another Example:
def get_int(msg, min=0, max=10):

Define a class:
class foo(object):
    def __init__(self, value):
        self.__value = value


The first argument (and only) in the class line is telling python what class to inherrit, and object is the bare minimum, and is default, so class foo: is the same, though not as clear.

Within the class you define methods (functions) that set behavior.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1