Page 1 of 1

# Connecting a menu with functions.

Posted 17 October 2019 - 09:13 AM

I have been trying to create a menu with options and connecting it to functions but have been unsuccessful. Here is what I have done so far,
```def mainMenu():
print("2. Find Factorial")
print("3. Find the Area")
print("4. Find the Circumference")
print("5. Exit")
while True:
try:
selection =int(input("Enter a Choice: "))
if selection==1:
elif selection==2:
factorial()
elif selection==3:
area()
elif selection==4:
circumference()
elif selection==5:
exit
else:
print("Invalid Choice, Enter 1-5")

except ValueError:
print("Invalid Choice, Enter 1-5")
print("**Create a Username**") #Message to state the use is creating a username.
print("enter name")
First = input("Enter your first name: ") #Asking the user for their first name.
Last = input("Enter your last name: ") # Asking the user for their last name.
first_letter = First #Creating a Variably and assigning the first initial to it.
Sur = Last #Creating a variable with a value of the last name.
#Username is taking the first initial of the first name and the entire surname.

print("Find The Factorial") #Message stating we are finding the Factorial.
def factorial(num):
if num == 1:
return num
else:
return num * factorial(num - 1) #The number entered mulitpled by the next number down and so on to 1.

num = int(input("Enter A Number: ")) #Asking the user to enter a number to find the factorial.

if num < 0:
print("Factorial Cannot Be Completed")
elif num == 0:
print("Factorial of 0 is 1")
else:
print("Factorial of", num, "is: ", factorial(num))

print("Finding The Area of A Triangle")#Find the area message.
def area():
#Find the area of a trianlge
print("Find the Area of a Triangle")
Width = int(input("Enter The Width in cm: ")) #Asking the user to input the Width.
Height = int(input("Enter The Height in cm: ")) #Asking the user to input the Height.
#Enter the Width and Height Dimensions of the triangle.
area = (Width * Height / 2) #Area Calculation, Width multiplied by Height then divided by 2.
print(area)

print("Find The Circumference of A Circle")#Find the circumference message.
def circumference():
#Find the circumference.

print("Find the Circumference")
Circumference  = Radius * 2 * 3.14 #Circumference calculation is radius times two then multiplied by pi or 3.14.
print(Circumference)

``` The 4 functions run but it misses out the menu at the start. Apologises for the terrible code I have only been doing this for about 3 weeks. So any help with this would be greatly appreciated,

Thank you,

Blair.

This post has been edited by modi123_1: 17 October 2019 - 09:14 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.

Is This A Good Question/Topic? 0

## Replies To: Connecting a menu with functions.

### #2 DK3250 Reputation: 561
• Posts: 1,791
• Joined: 27-December 13

## Re: Connecting a menu with functions.

Posted 17 October 2019 - 09:39 AM

Hi BlairHam92, welcome to Dream.In.Code.

There are a number of issues with your code.
The mainMenu() function is never called, so that's why is doesn't run.

The factorial function is made correctly, but the other isn't.
E.g. the area() function is only one single line; but you overwrite this function name in line 61 and never actually call the function.
Similar issues with the other functions.

I hope this will push you in the right direction...

## Re: Connecting a menu with functions.

Posted 17 October 2019 - 12:59 PM

Hey DK3250,

Thank you for your help and for the welcome.

Yeah I knew there would be a few issues with it, I have literally never done this before so it's all new to me.

I will have another look at it as when I run it the username, area, factorial and circumference sections all work just won't connect.

Thanks again.

Blair

### #4 baavgai • • Dreaming Coder
•   Reputation: 7501
• Posts: 15,544
• Joined: 16-October 07

## Re: Connecting a menu with functions.

Posted 17 October 2019 - 01:31 PM

You either missed an indent or are simply inviting chaos...

Take a step back.
```def username():
print("you called username - need code")

def factorial():
print("you called factorial - need code")

def area():
print("you called area - need code")

def circumference():
print("you called circumference - need code")

# this is really just main, but ok
done = False # this is for your while
# we'll start the while here
while not done:
# because you want to show these each time, right?
print("2. Find Factorial")
print("3. Find the Area")
print("4. Find the Circumference")
print("5. Exit")

try:
selection =int(input("Enter a Choice: "))
if selection==1:
elif selection==2:
factorial()
elif selection==3:
area()
elif selection==4:
circumference()
elif selection==5:
# this looks like a bug
# exit
# given then we're going to loop while done is false
# can you figure out what to do here?
else:
print("Invalid Choice, Enter 1-5")
except ValueError:
print("Invalid Choice, Enter 1-5")

```

That will run, though not do much. You can now work on getting each of your menu options to work as you like. Make sure all the code for each function is properly indented. If you want to test a function individually, comment out mainMenu and call that function directly. Take it slow, one section at a time.

Hope this helps.

## Re: Connecting a menu with functions.

Posted 03 December 2019 - 02:28 AM baavgai, on 17 October 2019 - 01:31 PM, said:

That will run, though not do much. You can now work on getting each of your menu options to work as you like. Make sure all the code for each function is properly indented. If you want to test a function individually, comment out mainMenu and call that function directly. Take it slow, one section at a time.

Hope this helps.

This is a way out to me as well, and I'd try to do thу same.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; } 