lundi 2 octobre 2017

If-Else statement pulling from functions is not reading the variable correctly

I cannot figure out why the if statement in the main() function is throwing error every time instead of checking the variable userChoice like it should. Unless it is and I am just missing something obvious.

import math

PI = 3.14159

def printGreeting():
    print('This program will perform calculations based on your choices')

#display menu for user to choose from
def menuDisplay():
    print ('Enter 1 to calculate the area of a circle')
    print ('Enter 2 to calculate the surface area of a sphere')
    print ('Enter 3 to calculate the volume of a sphere')
    print ('Enter 4 to quit')

#check the user's choice to make sure it is an int
def choiceCheck(userChoice):
    flag = True
    while(flag == True):

        if (userChoice.isdigit() == True):
            int(userChoice) 
            flag = False 
        elif (userChoice.isalpha() == True):
            print('Choice should be a number.')
            flag = False
        else: # because everything else has been checked, must be a float
            print('Choice should be an int, not float.')    
            flag = False

    return userChoice

#get the positive radius
def radiusF():
    flag = True
    radius = -1
    try:
        radius = int(input('Enter the radius: '))

        if (radius > 0):
            flag = False
        else:
            print ('The radius has to be positive')

    except ValueError:
        print ('Radius needs to be a number')

    return radius

#calculate the area        
def areaF(radius):
    area = PI * (radius * radius)
    print (area)

#calculate the surface area of a sphere
def surfaceAreaF(radius):
    surfaceArea = 4 * PI * (radius * radius)
    print (surfaceArea)


#calculate the volume of a sphere
def volumeF(radius):
    volume = (4/3 * PI) * (radius ** 3)
    print (volume)


def main():
    userChoice = -1

    printGreeting()
    menuDisplay()

    userChoice = input('Enter your choice: ')

    choiceCheck(userChoice)
    radius = radiusF()

This is where my issue arise, or at least shows itself by throwing an error even if userChoice is equal to 1,2, or 3. I know I am missing something but I cannot find it. Thank you for any help you may be able to offer.

    if (userChoice == 1):
        areaF(radius)
    elif (userChoice == 2):
        surfaceAreaF(radius)
    elif (userChoice == 3):
        volumeF(radius)
    else:
        print('Error')  


main()

Cheers

Aucun commentaire:

Enregistrer un commentaire