Monday, 2024-04-29, 1:36 PM
Welcome Guest

Distance Education

[ New messages · Members · Forum rules · Search · RSS ]
  • Page 1 of 1
  • 1
Forum moderator: Christos  
Forum » Software Development » RE:Work » How did you guys solve the week 10 Python activity? (I'm interested in how everyone solved it)
How did you guys solve the week 10 Python activity?
HaydenJohnsDate: Tuesday, 2009-05-05, 1:59 PM | Message # 1
Lieutenant colonel
Group: Checked
Messages: 123
Awards: 0
Reputation: 0
Status: Offline
I was thinking about this weeks course material, how it said we all have our own particular style we use to code. I'd like to see an example of this, so would people please post their solution to the painter program?

I sent it off with one rather annoying fatal bug in it unsure . If you give it a decimal value for the area, it dies.

So, how did you guys solve it?

Here's mine:

Added (2009-05-05, 1:59 Pm)
---------------------------------------------
#Paint Quote Program 1.3
#Written by Hayden Johns

#Gives a quote for the cost of to paint a house
#based on information given by the user.

#==================================================
#Program History:

#----------
#Changes from 1.1:
#----------
#Fatal bug in 'confirm details'
#Fixed thanks to the advice of the Ubuntu community

#Fatal bug in 'write to file'
#Fixed by substituting code from 'confirm details'.

#Spit and polish changes. Neatened output.

#----------
#Changes from 1.2:
#----------
#Added validation into input to prevent
#fatal errors occuring in data type conversion.

#More spit and polish changes. Added measurements to values (meters, $, etc.)

#==================================================

print "**************"
print "*Paint Quoter*"
print "**************"

#Set variables and constants
NUM="1234567890."

#Calculation Variables
totalcost = 0
totalarea = 0
area = None

#Sentry variables. Check contains unnaceptable values for integers
#and accept acts as a boolean.
check = 1
accept = 0

#Each room is stored as a tuple, which is stored in this list.
house = []

#Get adress from the user. Adress is constant after it is set.
address = raw_input("What is the Customer's Address?\n")

#keep adding rooms until the user stops

loop = ""
while loop.lower() !="n":
accept = 0
while accept == 0:
#Get information about room from the user
area = raw_input("\n\nWhat is the area to be painted in this room? (square meters)")

#check that input is valid before converting it.
check = ""
for char in area:
if char not in NUM:
check += char

if check == "": #If there are no illegal characters in the string, convert and escape the loop
area = int(area)
accept = 1
else: #If there are illegal characters in the string, generate an error message
print "Error. You must enter a number."
accept = 0

colour = raw_input("What colour is the paint?")

#calculates the price of individual room, and
#adds it to the total cost.
paintcost = 1
if colour.lower() != "white":
paintcost = 2
else:
paintcost = 1
cost = area * 5 * paintcost
totalcost += cost
totalarea += area

#Add room information into house
house += [(area, colour, cost),]

#querey if there are any more rooms. Break loop if answer is no.
loop = ""
while loop.lower() != "y" and loop.lower() != "n":
#prevents illegal answers. generates error message and asks again if the answer is not y or n
loop = raw_input("\nAre there any more rooms? (y/n)")
if loop.lower() != "y" and loop.lower() != "n":
print "\nERROR. Please type Y for Yes or N for No, and then press Enter."

#CONFIRM DETAILS
#Displays the details the user entered in the room on the screen.
print"\n-----------\nConfirm the details:\n"

rooms = len(house)
print "The address is", address
print "Total number of rooms is", rooms
print "The area to be painted in each of the rooms are:\n",
for a, c, p in house: #prints the area of each room (a is area, c is colour, p is price)
print str(a) + " meters square"
print "\nThe colours to be painted are:\n",
for a, c, p in house: #prints the colour of each room (a is area, c is colour, p is price)
print c
print "\nThe costs of each room are:\n",
for a, c, p in house: #prints the cost of each room (a is area, c is colour, p is price)
print "$" + str(p)
print "\nThe total area to be painted is " + str(totalarea) + " meters square"
print "\nThe total cost is $" + str(totalcost)

Added (2009-05-05, 1:59 Pm)
---------------------------------------------
#WRITE TO FILE
#Waits for the user to check the details and prompt the program,
#and then saves the detailsto a text file.
raw_input("Press enter to write to file, or press Ctrl+C to end.")
print "Writing to file..."

text_file = open("paintquote.txt", "w")

text_file.write("**************\n")
text_file.write("*Paint Quoter*\n")
text_file.write("**************\n")

text_file.write("Address:\n")
text_file.write(address)
text_file.write("\n\nNumber of rooms:\n")
text_file.write(str(rooms))
text_file.write("\n\nArea of each room:\n")
for a, c, p in house: #prints the area of each room (a is area, c is colour, p is price)
text_file.write(str(a)+"m, ")
text_file.write("\n\nColours to be painted in each room:\n")
for a, c, p in house: #prints the colour of each room (a is area, c is colour, p is price)
text_file.write(str©+", ")
text_file.write("\n\nCosts of each room:\n")
for a, c, p in house: #prints the cost of each room (a is area, c is colour, p is price)
text_file.write("$"+str(p)+", ")
text_file.write("\n\nTotal area: ")
text_file.write(str(totalarea) + " meters square")
text_file.write("\n\n----------\nTotal Cost: $")
text_file.write(str(totalcost))
text_file.close()

print "Done"


~ Click it
 
Dr4c0niusDate: Wednesday, 2009-05-06, 11:14 AM | Message # 2
Major
Group: Users
Messages: 96
Awards: 2
Reputation: 0
Status: Offline
I'm not going to post my code (because i wrote it on the friday/saturday border at one oclock in the morning after forgetting about it, and i don't think it would be terribly profitable to anyone biggrin ) but one thing you might like to try is implementing exceptions (try/except).

Basically the try encloses a bunch of code, and if it crashes or something happens it sends it to another specified bit of code. Its an alternate solution to checking a character set, and works for stuff like buffer overflows and other bits, but it slows the program down a lot.

Edit: it kills speed in c++. Im not sure about python.


Oderint dum metuant

<locokamil> Your belief system is thermodynamically unsound.

Message edited by Dr4c0nius - Wednesday, 2009-05-06, 3:32 PM
 
HaydenJohnsDate: Thursday, 2009-05-21, 11:27 AM | Message # 3
Lieutenant colonel
Group: Checked
Messages: 123
Awards: 0
Reputation: 0
Status: Offline
I'll pass on that if it kills speed. If I had the time, I'd try to convert to a floating point instead of an integer. Or, I could just take out "." From NUM to stop it from slipping through validation.

And the reason I wanted people to post was to show the difference in our styles of coding. Don't be shy wink

Added (2009-05-21, 11:27 Am)
---------------------------------------------
Hmm... Well this is a failure... dry


~ Click it
 
Forum » Software Development » RE:Work » How did you guys solve the week 10 Python activity? (I'm interested in how everyone solved it)
  • Page 1 of 1
  • 1
Search: