Jump to content

How to continue program from ending

- - - - -

  • Please log in to reply
3 replies to this topic

#1
mkjt88

mkjt88

    Newbie

  • Members
  • PipPip
  • 27 posts
Hey guys new to python and programming in general.. Got this simple area calculator program but have a few questions on it. Once you do a calculation it quits.. I would like it to ask to continue to another calculation or quit but I don't know how to re-call the program. Are there ways like functions or blocks I could put the code in to call it to run whenever I want like run this part again for another calculation.
#-------------------
# PRINT OUT THE MENU
print "Please select a shape"
print "1 - Rectangle"
print "2 - Triangle"
print "3 - Circle"


#----------------
# GETS USER INPUT
shape = input(">> ")


#-----------------
# CALCULATING AREA
    # RECTANGLE
if shape == 1:
    height = input("Enter the Height: ")
    width = input("Enter the Width: ")
    area = height*width
    print "The area is ", area

    # TRIANGLE
elif shape == 2:
    base = input("Enter Base Value: ")
    height = input("Enter Height: ")
    area = .5*base*height
    print "The area is ", area

    # CIRCLE
else:
    radius = input("Enter the Radius: ")
    area = 3.14*(radius**2)
    print "The area is ", area
I need a way to block sections of code to run when I call for them but not sure how to do this.. Here is the source let me know if you have any questions about what I am asking... thanks.

print "Area Calculation Program"
print "------------------------"
print
print


#-------------------
# PRINT OUT THE MENU
print "Please select a shape"
print "1 - Rectangle"
print "2 - Triangle"
print "3 - Circle"


#----------------
# GETS USER INPUT
shape = input(">> ")


#-----------------
# CALCULATING AREA
    # RECTANGLE
if shape == 1:
    height = input("Enter the Height: ")
    width = input("Enter the Width: ")
    area = height*width
    print "The area is ", area

    # TRIANGLE
elif shape == 2:
    base = input("Enter Base Value: ")
    height = input("Enter Height: ")
    area = .5*base*height
    print "The area is ", area

    # CIRCLE
else:
    radius = input("Enter the Radius: ")
    area = 3.14*(radius**2)
    print "The area is ", area


#2
mkjt88

mkjt88

    Newbie

  • Members
  • PipPip
  • 27 posts
Sorry for the double post but I already have another problem I forgot to address. Along with finding a way to call the program again.. As you can see now I added all the shapes in my elif statements and left the else statement in case someone chooses an option that is not valid. Now if they do choose something like say 6 which isn't an option.. how could I get it to show my print statement that is there but then also call the program to start again and ask to input your selection again. Thanks guys

print "Area Calculation Program"
print "------------------------"
print
print


#-------------------
# PRINT OUT THE MENU
print "Please select a shape"
print "1 - Rectangle"
print "2 - Square"
print "3 - Triangle"
print "4 - Circle"


#----------------
# GETS USER INPUT
shape = input(">> ")


#-----------------
# CALCULATING AREA
    # RECTANGLE
if shape == 1:
    height = input("Enter the Height: ")
    width = input("Enter the Width: ")
    area = height*width
    print "The area is ", area
        
        #SQUARE
elif shape == 2:
        size = input("Enter Height or Width: ")
        area = size*size
        print "The area is ", area
        
    # TRIANGLE
elif shape == 3:
    base = input("Enter Base Value: ")
    height = input("Enter Height: ")
    area = .5*base*height
    print "The area is ", area

    # CIRCLE
elif shape == 4:
        radius = input("Enter the Radius: ")
        area = 3.14*(radius**2)
        print "The area is ", area

else:
    print "Please select a valid option (1-4)"


#3
s1n4

s1n4

    Newbie

  • Members
  • Pip
  • 9 posts
print "Area Calculation Program"

print "------------------------\n"




#Create a function

def calculating() :

  #-------------------

  # PRINT OUT THE MENU

  print "\nPlease select a shape"

  print "1 - Rectangle"

  print "2 - Square"

  print "3 - Triangle"

  print "4 - Circle"

  #----------------

  # GETS USER INPUT

  shape = input(">> ")


  #-----------------

  # CALCULATING AREA

  # RECTANGLE

  if shape == 1:

    height = input("Enter the Height: ")

    width = input("Enter the Width: ")

    area = height*width

    print "The area is ", area

                

  #SQUARE

  elif shape == 2:

    size = input("Enter Height or Width: ")

    area = size*size

    print "The area is ", area

                

  # TRIANGLE

  elif shape == 3:

    base = input("Enter Base Value: ")

    height = input("Enter Height: ")

    area = .5*base*height

    print "The area is ", area


  # CIRCLE

  elif shape == 4:

    radius = input("Enter the Radius: ")

    area = 3.14*(radius**2)

    print "The area is ", area


  else:

     print "Please select a valid option (1-4)"

     

     

#Crate a loop for calling function

while True :

  calculating()

  #Create ask for continue

  if raw_input("Do you continue ? y/n >>") == "y" :

    continue

  else :

    break

Good Luck

#4
mkjt88

mkjt88

    Newbie

  • Members
  • PipPip
  • 27 posts
Thanks a lot exactly what I needed.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users