Python Forum
How to create a menu and execute a function based on user selection in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a menu and execute a function based on user selection in python?
#1
Hello, I am making a program that looks for the root of a polynomial by different methods (bisection, false position, raphson and secant).
I have the code of the individual programs where each one asks the user to enter the values necessary to perform the method. How can I implement all the programs in one and create a menu to select the program that the user wants to use?(that is for university course no problem use high level coding)
for example
Root find method
choose option
1
2
3
option#
***** Method
insert the funtion x**2+x+3
insert initial variable #
error#

(each program separate by #--------------------)
Also how to modify these codes to be able to enter a function instead of a fixed one.
#biseccion

def f(x):
    return x**3-5*x-9

# Implementing Bisection Method
def bisection(x0,x1,e):
    step = 1
    print('\n\n*** BISECTION METHOD IMPLEMENTATION ***')
    condition = True
    while condition:
        x2 = (x0 + x1)/2
        print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))

        if f(x0) * f(x2) < 0:
            x1 = x2
        else:
            x0 = x2
        
        step = step + 1
        condition = abs(f(x2)) > e

    print('\nRequired Root is : %0.8f' % x2)


# Input Section
x0 = input('First Guess: ')
x1 = input('Second Guess: ')
e = input('Tolerable Error: ')

# Converting input to float
x0 = float(x0)
x1 = float(x1)
e = float(e)



# Checking Correctness of initial guess values and bisecting
if f(x0) * f(x1) > 0.0:
    print('Given guess values do not bracket the root.')
    print('Try Again with different guess values.')
else:
    bisection(x0,x1,e)
#false---------------------------------------
def f(x):
    return x**3-5*x-9

# Implementing False Position Method
def falsePosition(x0,x1,e):
    step = 1
    print('\n\n*** FALSE POSITION METHOD IMPLEMENTATION ***')
    condition = True
    while condition:
        x2 = x0 - (x1-x0) * f(x0)/( f(x1) - f(x0) )
        print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))

        if f(x0) * f(x2) < 0:
            x1 = x2
        else:
            x0 = x2

        step = step + 1
        condition = abs(f(x2)) > e

    print('\nRequired root is: %0.8f' % x2)


# Input Section
x0 = input('First Guess: ')
x1 = input('Second Guess: ')
e = input('Tolerable Error: ')

# Converting input to float
x0 = float(x0)
x1 = float(x1)
e = float(e)

#Note: You can combine above two section like this
# x0 = float(input('First Guess: '))
# x1 = float(input('Second Guess: '))
# e = float(input('Tolerable Error: '))


# Checking Correctness of initial guess values and false positioning
if f(x0) * f(x1) > 0.0:
    print('Given guess values do not bracket the root.')
    print('Try Again with different guess values.')
else:
    falsePosition(x0,x1,e)
#-------------------------------------------------------
def f(x):
    return x**3 - 5*x - 9

# Defining derivative of function
def g(x):
    return 3*x**2 - 5

# Implementing Newton Raphson Method

def newtonRaphson(x0,e,N):
    print('\n\n*** NEWTON RAPHSON METHOD IMPLEMENTATION ***')
    step = 1
    flag = 1
    condition = True
    while condition:
        if g(x0) == 0.0:
            print('Divide by zero error!')
            break
        
        x1 = x0 - f(x0)/g(x0)
        print('Iteration-%d, x1 = %0.6f and f(x1) = %0.6f' % (step, x1, f(x1)))
        x0 = x1
        step = step + 1
        
        if step > N:
            flag = 0
            break
        
        condition = abs(f(x1)) > e
    
    if flag==1:
        print('\nRequired root is: %0.8f' % x1)
    else:
        print('\nNot Convergent.')


# Input Section
x0 = input('Enter Guess: ')
e = input('Tolerable Error: ')
N = input('Maximum Step: ')

# Converting x0 and e to float
x0 = float(x0)
e = float(e)

# Converting N to integer
N = int(N)


# Starting Newton Raphson Method
newtonRaphson(x0,e,N)
#-------------------------------------------------------
def f(x):
    return x**3 - 5*x - 9

# Implementing Secant Method

def secant(x0,x1,e,N):
    print('\n\n*** SECANT METHOD IMPLEMENTATION ***')
    step = 1
    condition = True
    while condition:
        if f(x0) == f(x1):
            print('Divide by zero error!')
            break
        
        x2 = x0 - (x1-x0)*f(x0)/( f(x1) - f(x0) ) 
        print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))
        x0 = x1
        x1 = x2
        step = step + 1
        
        if step > N:
            print('Not Convergent!')
            break
        
        condition = abs(f(x2)) > e
    print('\n Required root is: %0.8f' % x2)


# Input Section
x0 = input('Enter First Guess: ')
x1 = input('Enter Second Guess: ')
e = input('Tolerable Error: ')
N = input('Maximum Step: ')

# Converting x0 and e to float
x0 = float(x0)
x1 = float(x1)
e = float(e)

# Converting N to integer
N = int(N)


# Starting Secant Method
secant(x0,x1,e,N)
Reply
#2
There was a very recent post on how to create a menu that sould be helpful, here
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  create a function format_date ? Kessie1971 7 1,799 Apr-24-2023, 11:54 AM
Last Post: Larz60+
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,291 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  i need help at the part where i have to use a function and initialise the menu. imbacoconut 5 1,586 Jun-02-2022, 09:19 PM
Last Post: jefsummers
  Changing Directory based on user input paulmerton4pope 13 8,059 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  sys.stdin to do a word count based on user entry Kaltex 3 3,693 Jul-19-2020, 01:54 PM
Last Post: deanhystad
  create a function that can count polk203 1 1,666 Apr-12-2020, 12:13 PM
Last Post: ibreeden
  How to make the function to prompt the user repeatedly till he enters a no sbabu 3 2,426 Mar-26-2020, 10:04 PM
Last Post: Blackdog31
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,818 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,519 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  create function let_to_num() Truman 1 6,030 Feb-15-2018, 04:04 AM
Last Post: ka06059

Forum Jump:

User Panel Messages

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