0

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

Dakota Brown
  • 57
  • 1
  • 2
  • 7

1 Answers1

0

In your choiceCheck() function, int(userChoice) returns an integer, it doesn't convert (not typecast, thanks to @Mad Physicist) userCast into integer. To typecast it:

userChoice = int(userChoice) 

Or if you don't want to typecast it, change the numbers in if conditions of main() function into strings.

Rahul Bharadwaj
  • 2,555
  • 2
  • 18
  • 29
  • 3
    Nitpick: there is no such thing as type casting in Python. `int(...)` is not casting anything. It is calling the constructor of the `int` class. – Mad Physicist Oct 02 '17 at 17:40
  • Well I made the userChoice = int(userChoice) change and it still skips down to the else: print ('error') – Dakota Brown Oct 02 '17 at 17:54