I have a program that is for conversions of simple metrics. I'm a super noob to python and could use a little help.
def main():
use_menu()
if feet_inches():
def use_menu():
get_menu()
selection = int(input('Please make a menu selection:'))
if selection == 1:
feet_inches()
elif selection == 2:
yards_feet()
elif selection == 3:
miles_yards()
elif selection == 4:
miles_feet()
def get_menu():
print('1. Convert feet to inches')
print('2. Convert yards to feet')
print('3. Convert miles to yards')
print('4. Convert miles to feet')
print('5. Exit')
def feet_inches():
feet2inches = int(input('Enter the number of feet:'))
answer1 = feet2inches / 3
return answer1
def yards_feet():
yards2feet = int(input('Enter the number of yards:'))
answer2 = yards2feet * 3
return answer2
def miles_yards():
miles2yards = int(input('Enter the number of miles:'))
answer3 = miles2yards * 1760
return answer3
def miles_feet():
miles2feet = int(input('Enter the number of miles:'))
answer4 = miles2feet * 5280
return answer4
main()
How do I print the returned values in my main function? I keep getting unresolved reference when I try to set answer1 to a variable in the main function. I'm also struggling with how to write the if/elif for that. Just a little nudge in the right direction would be appreciated.
CodePudding user response:
You can use return statements in the use_menu
aswell. And then set the answer equal to what is returned by that function in the main function.
def main():
answer = use_menu()
print(answer)
def use_menu():
get_menu()
selection = int(input('Please make a menu selection:'))
if selection == 1:
return feet_inches()
elif selection == 2:
return yards_feet()
elif selection == 3:
return miles_yards()
elif selection == 4:
return miles_feet()
CodePudding user response:
You could change your code as follows:
def main():
# First, retrieve function to be applied
func = use_menu()
if func is not None:
# Apply function - Could be done in one liner
res = func()
print(res)
def use_menu():
get_menu()
selection = int(input('Please make a menu selection:'))
if selection == 1:
return feet_inches
elif selection == 2:
return yards_feet
elif selection == 3:
return miles_yards
elif selection == 4:
return miles_feet
else:
return None
CodePudding user response:
Line 3:
if feet_inches():
there is no conditional action underneath.
You could try something like this: Remove line 3 above.
def use_menu():
get_menu()
selection = int(input('Please make a menu selection:'))
if selection == 1:
fi = feet_inches()
print(fi)
elif selection == 2:
yf = yards_feet()
print(yf)
elif selection == 3:
my = miles_yards()
print(my)
elif selection == 4:
mf = miles_feet()
print(mf)
CodePudding user response:
You can use a switch for the use_menu.
Also you can just do this to print the values...
def feet_inches():
feet2inches = int(input('Enter the number of feet:'))
answer1 = feet2inches / 3
return answer1
print(feet_inches())