Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can someone please help me?
#1
I need to write this simple code for my python class. It has stumped me and I cant find any help through google searching.

My problem is, when I try to select an option from my menu, the menu gets displayed again. Any help would be appreciated!

import math

a = None

def Main():
    while True:
        choice = input(
        """
Welcome to my calculation program! please choose from the following options.

1. Find the area of a circle
2. Find the circumfrence of a circle
3. Find the area of a rectangle
4. find the perimeter of a rectangle
5. Exit the program

"""
        )
    if choice == 1:
        CircleArea()
    elif choice == 2:
        CircleCircumf()
    elif choice == 3:
        RectangleArea()
    elif choice == 4:
        RectangleParam()
    elif choice == 5:
        exit
    

def CircleArea():
    radius = input("\nPlease input the radius of the circle")
    area = math.pi*r*r
    print ("\nThe area of your circle is ", area)
    while True:
        choice = input ("Would you like to return to the main menu? y/n")
    if choice == y:
        Main()
    elif choice == n:
        exit


def CircleCircumf():
    radius = input("\nPlease input the radius of the circle")
    circumf = 2 * math.pi * radius
    print ("\nThe circumference of the circle is", circumf)
    while True:
        choice = input ("Would you like to return to the main menu? y/n")
    if choice == y:
        Main()
    elif choice == n:
        exit


def RectangleArea():
    width = input("\nHow wide is the rectangle?")
    height = input("\nHow high is the rectange?")
    area = width * height
    print("The area of the rectangle is", area)
    while True:
        choice = input ("Would you like to return to the main menu? y/n")
    if choice == y:
        Main()
    elif choice == n:
        exit


def RectangleParam():
    width = input("\nHow wide is the rectangle?")
    height = input("\nHow high is the rectange?")
    param = width + width + height + height
    print("\nThe perimeter of the rectangle is", param)
    while True:
        choice = input ("Would you like to return to the main menu? y/n")
    if choice == y:
        Main()
    elif choice == n:
        exit

Main()
Reply
#2
Your if/elif statements need to be part of the loop. Their current indentation has them run after the loop. Indent that whole section once.

Also, if you are using Python 3.x, which you should, input returns a string. You are comparing that string to integers, which will always be false. You can either convert choice to an integer with int(), or you can compare to strings (if choice == '1':)

Also, to get out of a loop, use break, not exit.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Oct-09-2017, 03:11 AM)ichabod801 Wrote: Your if/elif statements need to be part of the loop. Their current indentation has them run after the loop. Indent that whole section once.

Also, if you are using Python 3.x, which you should, input returns a string. You are comparing that string to integers, which will always be false. You can either convert choice to an integer with int(), or you can compare to strings (if choice == '1':)

Also, to get out of a loop, use break, not exit.

Thank you sir, i will give this a shot

Okay so i replaced exit with break like so:

def CircleArea():
    radius = input("\nPlease input the radius of the circle")
    area = math.pi*r*r
    print ("\nThe area of your circle is ", area)
    while True:
        choice = input ("Would you like to return to the main menu? y/n")
    if choice == y:
        Main()
    elif choice == n:
        break
Now I am getting an error saying my break is outside the loop?
Reply
#4
Reread my first paragraph.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Okay I got it. I have many more errors I need to iron out. Thank you for your guidance!

If I may make another inquiry, I seem to have a infinite loop in here.
I added a new def at the end of my code to print a thank you message, and when that option is chosen, it loops the message.

I also tweaked my main code, it now looks like this:

def Main():
    print(
        """
Welcome to my calculation program! please choose from the following options.

1. Find the area of a circle
2. Find the circumfrence of a circle
3. Find the area of a rectangle
4. find the perimeter of a rectangle
5. Exit the program

"""
        )
    choice = input("\n:")
    while True:
        if choice == "1":
            CircleArea()
        elif choice == "2":
            CircleCircumf()
        elif choice == "3":
            RectangleArea()
        elif choice == "4":
            RectangleParam()
        elif choice == "5":
            End()


The End() is this:

def End():
    print("\n\nThank you for using my program! Have a good day")
Reply
#6
As I said, you use the break statement to get out of a loop. Put one right after you call end.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020