PDA

View Full Version : Python help.



Cha0tic
05-04-2014, 01:52 PM
Hey brahs working on some HW and having a issue with my code, my second set of if and elif statesment are not executing.
They aren't giving me any errors its just as if they are being skipped over.

Here's the code and mind checking if I'm doing what is required.

def main():
#input section
print ("Caluclate the Volume of: ")
print ("\t", "1)Box\n"
"\t", "2)Cylinder\n"
"\t", "3)Pyramid\n"
"\t", "4)Sphere\n"
"\t", "5)Exit\n")
choice = int(input("Enter Choice :"))

if choice == 1:
print ("You choose to calculate the volume of a Box.")
boxlength = float(input("Enter Length of the box: " ))
boxwidth = float(input("Enter Width of the Box : "))
boxheight = float(input("Enter Height of the Box : "))



elif choice == 2:
print ("You choose to calculate the volume of a Cylinder.")
cylinderradius = float(input("Enter Radius of the Cylinder: "))
cylinderheight = float(input("Enter Height of the Cylinder: "))

elif choice == 3:
print("You choose to calculate the volume of a Pyramid.")
pybase = float(input("Enter the Base of the Pyramid : "))
pyheight = float(input("Enter the Height of the Pyramid : "))
elif choice == 4:
print ("You choose to caluclate the volume of a Sphere.")
sphereradius = float(input("Enter the Radius of the Sphere : "))

elif choice == 5:
print("Press any key to exit.")
while choice > 5:
print ("Not Valid Choice")
choice = int(input("Enter Choice :"))





#processing section
from math import math
from fractions import Fraction

boxvolume = length*width*height
cylindervolume = math.pi*(radius ** 2)*height
pyramidvolume = Fraction(1, 3)* (base ** 2) * height
spherevolume = Fraction(4, 3) * math.pi * radius**3







#output section
if choice == 1:
print ("Calculating the Volume of a Box\n"
"With a Length of", boxlength, "With a Width of", boxwidth, "With a Height of", boxheight)
elif choice == 2:
print("Calculating the Volume of a Cylinder\n"
"With a Radius of",cylinderradious, "With a Height", cylinderheight)
elif choice == 3:
print("Calculating the Volume of a Pyramid\n"
"With a Base of", pybase, "With the Height of", pyheight)
elif choice == 4:
print("Calculating the Volume of a Sphere\n"
"With a Radius", sphereradius)




main ()
print("Press any key to continue.")








Requirements


Requirements:
Comment thoroughly; add comments to identify the variables used and comments explaining each function.
Relevant data must be Input with proper prompts.
if .. elif must handle all possible inputs, validate menu choice input
Use a function to handle the menu.
Use a function to handle each shape.
Use upper() method wherever is necessary.
Each function prints the data related to it.
The menu() function prints the message and returned volume.
Printed results should be self explanatory and sufficient.

Cha0tic
05-04-2014, 04:23 PM
bump

Kapetan
05-04-2014, 04:31 PM
Python is such a horrible language. Did you try debugging it?

massover
05-04-2014, 04:36 PM
The "processing section" and "output section" are contained within the while loop causing them to not get executed.




[...]
elif choice == 5:
print("Press any key to exit.")
while choice > 5:

print ("Not Valid Choice")
choice = int(input("Enter Choice :"))

#processing section
from math import math
from fractions import Fraction

boxvolume = length*width*height
cylindervolume = math.pi*(radius ** 2)*height
pyramidvolume = Fraction(1, 3)* (base ** 2) * height
spherevolume = Fraction(4, 3) * math.pi * radius**3

#output section
if choice == 1:
print ("Calculating the Volume of a Box\n"
"With a Length of", boxlength, "With a Width of", boxwidth, "With a Height of", boxheight)
elif choice == 2:
print("Calculating the Volume of a Cylinder\n"
"With a Radius of",cylinderradious, "With a Height", cylinderheight)
elif choice == 3:
print("Calculating the Volume of a Pyramid\n"
"With a Base of", pybase, "With the Height of", pyheight)
elif choice == 4:
print("Calculating the Volume of a Sphere\n"
"With a Radius", sphereradius)


main ()
print("Press any key to continue.")


You didn't satisfy these requirements:
• if .. elif must handle all possible inputs, validate menu choice input
• Use a function to handle the menu.
• Use a function to handle each shape.
• Use upper() method wherever is necessary.
• Each function prints the data related to it.
• The menu() function prints the message and returned volume.

Try to figure it out, ask more questions if you still need help.

Cha0tic
05-04-2014, 04:38 PM
Python is such a horrible language. Did you try debugging it?

No I haven't even tried that.


The "processing section" and "output section" are contained within the while loop causing them to not get executed.




[...]
elif choice == 5:
print("Press any key to exit.")
while choice > 5:

print ("Not Valid Choice")
choice = int(input("Enter Choice :"))

#processing section
from math import math
from fractions import Fraction

boxvolume = length*width*height
cylindervolume = math.pi*(radius ** 2)*height
pyramidvolume = Fraction(1, 3)* (base ** 2) * height
spherevolume = Fraction(4, 3) * math.pi * radius**3

#output section
if choice == 1:
print ("Calculating the Volume of a Box\n"
"With a Length of", boxlength, "With a Width of", boxwidth, "With a Height of", boxheight)
elif choice == 2:
print("Calculating the Volume of a Cylinder\n"
"With a Radius of",cylinderradious, "With a Height", cylinderheight)
elif choice == 3:
print("Calculating the Volume of a Pyramid\n"
"With a Base of", pybase, "With the Height of", pyheight)
elif choice == 4:
print("Calculating the Volume of a Sphere\n"
"With a Radius", sphereradius)


main ()
print("Press any key to continue.")


You didn't satisfy these requirements:
• if .. elif must handle all possible inputs, validate menu choice input
• Use a function to handle the menu.
• Use a function to handle each shape.
• Use upper() method wherever is necessary.
• Each function prints the data related to it.

Try to figure it out, ask more questions if you still need help.

Mind explaining a bit in depth for my slow self?

And the upper method doesn't that just upper case the word/string?

Kapetan
05-04-2014, 04:42 PM
No I haven't even tried that.


Learn how to debug now. You will save more time by learning to debug now in the long run.
I don't know which IDE you are using, but trust me, take your time and google: 'how to debug in python'.

Problem fixing in code will never be easier.

massover
05-04-2014, 04:49 PM
Mind explaining a bit in depth for my slow self?

Can you try and clarify what you're confused about?

And the upper method doesn't that just upper case the word/string?

That is correct. I can't imagine what they want you to do with upper. But they are expecting something, and you haven't used it. Think about how you used it in class

^^^

cheesecake93
05-04-2014, 04:59 PM
-indent properly, so everything isn't caught in your while loop
-you're not using functions to handle each shape, def getCircleArea(radius): .. etc, in fact none of your required functions are defined.. the menu function should take in an input (which shape to calc for) and use one of the shape functions to calculate area

you basically have to restructure your code to something similar:

def main(): #your main function, basically only calls upper(), possibly loops it
def upper(): asks for user input (select which shape) depending on which shape it calls one of the shape functions to calc the area and print it
def box(r)/cylinder(r)/spehere(r)/pyramid(r): called from within upper function depending on user input, ask for size and calculate the volume

and yeah just like the others have said .upper() is a string method.. not sure what you would use it for here but be careful (I'm guessing they wanted shape input to be in string form and you're supposed to change the input string to upper case in order for it be case indifferent).
So


choice = raw_input("what shape?: ")
choice = choice.upper() #or is it just choice.upper() ? I don't remember haven't used py in a few months
if (choice = "CIRCLE"):
circleFindArea()
elif.....

Cha0tic
05-04-2014, 05:12 PM
Learn how to debug now. You will save more time by learning to debug now in the long run.
I don't know which IDE you are using, but trust me, take your time and google: 'how to debug in python'.

Problem fixing in code will never be easier.

I will get on that.


^^^

How my processing and output are apart/stuck in the loop that I first created.
All he used it was instead of upper casing a word he used the upper.function, its pointless but I suppose I better use it so I don't get points taken off.


-indent properly, so everything isn't caught in your while loop
-you're not using functions to handle each shape, def getCircleArea(radius): .. etc, in fact none of your required functions are defined.. the menu function should take in an input (which shape to calc for) and use one of the shape functions to calculate area

you basically have to restructure your code to something similar:

def main(): #your main function, basically only calls upper(), possibly loops it
def upper(): asks for user input (select which shape) depending on which shape it calls one of the shape functions to calc the area and print it
def box(r)/cylinder(r)/spehere(r)/pyramid(r): called from within upper function depending on user input, ask for size and calculate the volume

and yeah just like the others have said .upper() is a string method.. not sure what you would use it for here but be careful (I'm guessing they wanted shape input to be in string form and you're supposed to change the input string to upper case in order for it be case indifferent).
So


choice = raw_input("what shape?: ")
choice = choice.upper() #or is it just choice.upper() ? I don't remember haven't used py in a few months
if (choice = "CIRCLE"):
circleFindArea()
elif.....


I don't quite understand using a function to handle the menu and shapes.
I tried asking for help, on my last home work assignment and I got told stop wasting his time for being confused and asking for help. So didn't even bother trying to ask for help.

massover
05-04-2014, 05:41 PM
How my processing and output are apart/stuck in the loop that I first created.
All he used it was instead of upper casing a word he used the upper.function, its pointless but I suppose I better use it so I don't get points taken off.


Python uses spacing to indicate if code is included in a conditional(while/if) block. any indented lines of code after a conditional statement are included in that conditional code block.
example:



x=0
#this is a conditional while statement:
while x < 10:
#any code indented is included in the while statement.
print 'inside the while loop'
x = x + 1

#this code is not included in the while loop
print 'done with the while loop'


Does that help you see the issue in your while loop?

Cha0tic
05-04-2014, 06:14 PM
Python uses spacing to indicate if code is included in a conditional(while/if) block. any indented lines of code after a conditional statement are included in that conditional code block.
example:



x=0
#this is a conditional while statement:
while x < 10:
#any code indented is included in the while statement.
print 'inside the while loop'
x = x + 1

#this code is not included in the while loop
print 'done with the while loop'


Does that help you see the issue in your while loop?

I dedented the coding after that, if that's what you're trying to say.
Yes and no I don't really see how that applies to this specific code or how to apply it to this code.

Also getting a error from math

line 53, in main
from math import math

ImportError: cannot import name 'math'

Cha0tic
05-05-2014, 07:38 AM
bump of peace, any brahs mind fixing up the code so I can compare with it my original code.

Spetsnazos
05-05-2014, 07:51 AM
Python looks awful. Why do instructors force people to learn bs languages.

Cha0tic
05-05-2014, 08:51 AM
Python looks awful. Why do instructors force people to learn bs languages.

Whats worse is when you're confused and can't even ask the professor for help because you get told to stop wasting his time.

Cha0tic
05-05-2014, 02:11 PM
bump brahs

Cha0tic
05-05-2014, 02:34 PM
Added functions but getting errors with the process sectiong.


def main():
#input section
print ("Caluclate the Volume of: ")
print ("\t", "1)Box\n"
"\t", "2)Cylinder\n"
"\t", "3)Pyramid\n"
"\t", "4)Sphere\n"
"\t", "5)Exit\n")
choice = int(input("Enter Choice :"))

if choice == 1:
print ("You choose to calculate the volume of a Box.")
boxlength = float(input("Enter Length of the box: " ))
boxwidth = float(input("Enter Width of the Box : "))
boxheight = float(input("Enter Height of the Box : "))



elif choice == 2:
print ("You choose to calculate the volume of a Cylinder.")
cylinderradius = float(input("Enter Radius of the Cylinder: "))
cylinderheight = float(input("Enter Height of the Cylinder: "))

elif choice == 3:
print("You choose to calculate the volume of a Pyramid.")
pybase = float(input("Enter the Base of the Pyramid : "))
pyheight = float(input("Enter the Height of the Pyramid : "))
elif choice == 4:
print ("You choose to caluclate the volume of a Sphere.")
sphereradius = float(input("Enter the Radius of the Sphere : "))

elif choice == 5:
print("Press any key to exit.")

while choice > 5:
print ("Not Valid Choice")
choice = int(input("Enter Choice :"))







#processing section
import math
from fractions import Fraction

def boxvolume():
boxvolume = boxlength *boxwidth *boxheight
boxvolume()
def cylindervolume():
cylindervolume = math.pi*(cylinderradius ** 2)*cylinderheight
cylindervolume()
def pyramidvolume():
pyramidvolume = Fraction(1, 3)* (pybase ** 2) * pyheight
pyramidvolume()
def spherevolume():
spherevolume = Fraction(4, 3) * math.pi * sphereradius**3
spherevolume()








#output section

if choice == 1:
print ("Calculating the Volume of a Box\n"
"With a Length of", boxlength, "With a Width of", boxwidth, "With a Height of", boxheight,"\n"
"The Volume of the Box is", boxvolume)
elif choice == 2:
print("Calculating the Volume of a Cylinder\n"
"With a Radius of",cylinderradious, "With a Height", cylinderheight,"\n"
"The Volume of the Cylinder is",cylindervolume)
elif choice == 3:
print("Calculating the Volume of a Pyramid\n"
"With a Base of", pybase, "With the Height of", pyheight,"\n"
"The Volume of the Pyramid is",pyramidvolume)
elif choice == 4:
print("Calculating the Volume of a Sphere\n"
"With a Radius", sphereradius,"\n"
"The volume of the Sphere is",spherevolume)




main ()
print("Press any key to continue.")

Cha0tic
05-05-2014, 02:40 PM
And errors.
Errors


File "C:\Users\Christopher\Desktop\volume func.py", line 99, in <module>
main ()
File "C:\Users\Christopher\Desktop\volume func.py", line 62, in main
cylindervolume()
File "C:\Users\Christopher\Desktop\volume func.py", line 61, in cylindervolume
cylindervolume = math.pi*(cylinderradius ** 2)*cylinderheight
NameError: free variable 'cylinderradius' referenced before assignment in enclosing scope
>>>

Cha0tic
05-05-2014, 06:54 PM
If any brahs out there, fix this mess for me please. And also code from above, this one is some book work trying to get ready for the finals but book is no help either.
Been phucking with this chit trying to make it stop looping after I would enter the coupon values, it would look back to item cost.
I phucked it up big time, litereally begging for help, been at this for a few hours don't know how to fix it.


def main():
#input section
grandtotal = 0
answer = input("do you have a customer Y/N ")
while answer != "y" and answer != "n":
print("Not a valid answer!")
answer = input("do you have a customer Y/N ")

if answer == "y":
total = 0
numberitem = 0
amount = float(input("Enter items cost : "))

while (amount != 0):
total += amount
numberitem += 1
amount = float(input("Enter items cost : "))

while amount == 0:
print ("The number of items", numberitem);
print ("The total is ",format(total,"6.2f"))
coupon = input("Do you have any coupons?")
while coupon == "y":
couponamount = int(input("Enter amount of coupons: "))
coupondiscount = float(input("Enter the Value of Coupons"))
if coupondiscount != 0:
print("Proceed")
if coupon == "n":
amount = amount +1

print("Proceed without coupons.")



















#processing section
if coupon == "y":
coupontotal = couponamount * coupondiscount
grandtotal = total-coupontotal



#output section
from random import randint
print("Customer Bill Number:",randint(1,9999))



main()
input("Press any key to continue.")

happytrails
05-05-2014, 09:28 PM
Python is such a horrible language. Did you try debugging it?
Why do you say Python is so horrible?


Python looks awful. Why do instructors force people to learn bs languages.
Why do you say python looks awful and that its a bs language?