4 Replies - 352 Views - Last Post: 03 December 2019 - 02:28 AM Rate Topic: -----

#1 BlairHam92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-October 19

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("1. Create Username")
    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:
                username()
            elif selection==2:
                factorial()
            elif selection==3:
                area()
            elif selection==4:
                circumference()
            elif selection==5:
                exit
            else:
                print("Invalid Choice, Enter 1-5")
                mainMenu()
    
        except ValueError:
         print("Invalid Choice, Enter 1-5")
print("**Create a Username**") #Message to state the use is creating a username.
def 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[0] #Creating a Variably and assigning the first initial to it.
Sur = Last #Creating a variable with a value of the last name.
username = (first_letter + Sur) #Creating the username which adds the first initial and last name.
print("**This is Your Username**")
print(username)
#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")
Radius = int(input("Enter the radius in cm: ")) #Asking the user to enter the Radius value.
Circumference  = Radius * 2 * 3.14 #Circumference calculation is radius times two then multiplied by pi or 3.14.
print(Circumference)

:code:

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   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

#3 BlairHam92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-October 19

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
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7501
  • View blog
  • 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
def mainMenu():
    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("1. Create Username")
        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:
                username()
            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")
                # no mainMenu()
        except ValueError:
         print("Invalid Choice, Enter 1-5")
    print("exiting menu, done")

mainMenu() # start your program



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.
Was This Post Helpful? 0
  • +
  • -

#5 Histiogge   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 03-December 19

Re: Connecting a menu with functions.

Posted 03 December 2019 - 02:28 AM

View Postbaavgai, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1