Reputation: 101
This is my code: I explain the problem I'm having inside the file with comments. I was able to identify the problem, but I don't know how to solve it without changing the "define as many functions as possible" approach. Maybe define another function that asks for input if program has no value stored in memory? Unfortunately I was unable to fix it on my own. Big thanks to all!
#import math package to use math.pi for the value of PI
import math
def cylinder(r='r', h='h', calculate="none"):
r = call_input('r')
h = call_input('h')
if calculate == 's_area':
return 2*math.pi*pow(r,2)*h
elif calculate == 'volume':
return math.pi*pow(r,2)*h
def call_input(parameter):
if parameter == 'r':
return float(input('r = '))
elif parameter == 'h':
return float(input('h = '))
elif parameter == 'w': #line is useless atm but in the future more shapes ll be added
return float(input('w = '))
def output(key, choice):
if choice == 'A':
if key == 's_area':
return cylinder(calculate='s_area')
if key == 'volume':
return cylinder(calculate='volume')
def figure_prefix():
user_set_shape = input("Which geometric shape would you like to calculate? Type 'A' for cylinder: ").upper()
return user_set_shape
def main():
print()
print("Welcome! You're now using GeometricCalcultator:")
print()
user_choice = figure_prefix()
print(output('s_area', user_choice))
print(output('volume', user_choice)) #I understand that 'output' calls 'cylinder' that calls 'call_input'
#so this is why input for 'r' and 'h' is asked twice.
#However in my case it's important to not hardcode this inside 'main'
if __name__ == '__main__': #since in the future more shapes will be added and ea might require diff. inputs
main() #Program was to ask for input depending on the shape was chosen by user.
#Problem has to be fixed, following the same coding logic. That means define functions
#whenever possible and avoid hardcoding inside 'main()'.
Terminal output for r = h = 5 :
# Welcome! You're now using GeometricCalcultator 5,000,000! :)
# Which geometric shape would you like to calculate? Type 'A' for cylinder: A
# r = 5
# h = 5
# 785.3981633974483
# r = 5 **#< Those are the problematic lines. Program shouldn't ask for input twice!**
# h = 5 **#<**
# 392.69908169872417
Upvotes: 0
Views: 95
Reputation: 5694
This is the fix which prevents the input
being asked twice.
#import math package to use math.pi for the value of PI
import math
def cylinder(r, h, calculate="none"):
#r = call_input('r')
#h = call_input('h')
if calculate == 's_area':
return 2*math.pi*pow(r,2)*h
elif calculate == 'volume':
return math.pi*pow(r,2)*h
def call_input(parameter):
if parameter == 'r':
return float(input('r = '))
elif parameter == 'h':
return float(input('h = '))
elif parameter == 'w': #line is useless atm but in the future more shapes ll be added
return float(input('w = '))
def output(key, choice):
if choice == 'A':
r = float(input('the radius >> '))
h = float(input('the height >> '))
if key == 's_area':
return cylinder(r, h, calculate='s_area')
if key == 'volume':
return cylinder(r, h, calculate='volume')
def figure_prefix():
user_set_shape = input("Which geometric shape would you like to calculate? Type 'A' for cylinder: ").upper()
return user_set_shape
def main():
print()
print("Welcome! You're now using GeometricCalcultator:")
print()
user_choice = figure_prefix()
to_get = input('s_area or volume >> ')
print(output(to_get, user_choice))
#print(output('volume', user_choice)) #I understand that 'output' calls 'cylinder' that calls 'call_input'
#so this is why input for 'r' and 'h' is asked twice.
#However in my case it's important to not hardcode this inside 'main'
if __name__ == '__main__': #since in the future more shapes will be added and ea might require diff. inputs
main() #Program was to ask for input depending on the shape was chosen by user.
#Problem has to be fixed, following the same coding logic. That means define functions
#whenever possible and avoid hardcoding inside 'main()'.
in particular:
output
function once.to_get = input('s_area or volume >> ')
cylinder
function is modified to remove call_input
Upvotes: 0
Reputation: 5694
Here is alternative code based on your comment: There is now only one answer with the tuple (s_area, volume) returned.
#import math package to use math.pi for the value of PI
import math
def cylinder(r, h):
area = 2*math.pi*pow(r,2)*h
volume = math.pi*pow(r,2)*h
return area, volume
def call_input(parameter):
if parameter == 'r':
return float(input('r = '))
elif parameter == 'h':
return float(input('h = '))
elif parameter == 'w': #line is useless atm but in the future more shapes ll be added
return float(input('w = '))
def output(choice):
if choice == 'A':
r = float(input('the radius >> '))
h = float(input('the height >> '))
return cylinder(r, h)
def figure_prefix():
user_set_shape = input("Which geometric shape would you like to calculate? Type 'A' for cylinder: ").upper()
return user_set_shape
def main():
print()
print("Welcome! You're now using GeometricCalcultator:")
print()
user_choice = figure_prefix()
print(output(user_choice))
#print(output('volume', user_choice)) #I understand that 'output' calls 'cylinder' that calls 'call_input'
#so this is why input for 'r' and 'h' is asked twice.
#However in my case it's important to not hardcode this inside 'main'
if __name__ == '__main__': #since in the future more shapes will be added and ea might require diff. inputs
main() #Program was to ask for input depending on the shape was chosen by user.
#Problem has to be fixed, following the same coding logic. That means define functions
#whenever possible and avoid hardcoding inside 'main()'.
You will see that this solution is even simpler (fewer lines of code than the previous). Please accept your preferred answer with a tick.
Upvotes: 1