Reputation: 11
I am learning Python for the first time. I have a syntax error and I can not figure out if I have coded wrong or if I have just had a simple typo. I have pulled the menu out and put into a new window and the syntax error is something to do with my coding of the menu. I believe it is somewhere near the coding display menu(). If the answer it found I would greatly appreciate an explanation of why it was incorrect. (There may be some other errors in the coding but, I am stuck on the menu. After that I will work on any other issues that come up. Suggestions though would be appreciated.)
#This program allow for the user to convert weight managements
#Import Modules
import KilogramConversion
import PoundConversion
import TonConversion
#Menu choice constants
CONVERT_KILOGRAMS_OUNCE=1
CONVERT_KILOGRAMS_POUND=2
CONVERT_POUND_OUNCE=3
CONVERT_POUND_TON=4
CONVERT_TON_POUND=5
CONVERT_TON_OUNCE=6
CONVERT_TON_KILOGRAMS=7
QUIT_CHOICE=8
# Main Function
def main():
choice=0
while choice != QUIT_CHOICE:
display menu()
choice=int(input('Enter your choice:'))
#Perform menu choice
if choice == CONVERT_KILOGRAMS_OUNCE:
kilo=float(input("Enter weight in Kilograms:"))
print('The weight in ounces is:', KilogramConversion.ounce(kilo))
elif choice == CONVERT_KILOGRAMS_POUND:
kilo=float(input("Enter weight in Kilograms:"))
print('The weight in pounds is:', KilogramConversion.pound(kilo))
elif choice == CONVERT_POUND_OUNCE:
lbs=float(input("Enter weight in Pounds:"))
print('The weight in ounces is:', PoundConversion.ounce(lbs))
elif choice == CONVERT_POUND_TON:
lbs=float(input("Enter weight in Pounds:"))
print('The weight in tons is:', PoundConversion.ton(lbs))
elif choice == CONVERT_TON_POUND:
tons= float(input("Enter weight in Tons:"))
print('The weight in pounds is:', TonConversion.pound(tons))
elif choice == CONVERT_TON_OUNCE:
tons= float(input("Enter weight in Tons:"))
print('The weight in ounces is:', TonConversion.ounce(tons))
elif choice == CONVERT_TON_KILOGRAMS:
tons= float(input("Enter weight in Tons:"))
print('The weight in kilograms is:', TonConversion.kilogram(tons))
elif choice == QUIT_CHOICE:
print('Exiting...')
else:
print('Invalid Selection')
def display menu():
print(' ')
print(' ')
print(' Menu')
print('1. Kilograms to Ounce')
print('2. Kilograms to Pound')
print('3. Pound to Ounce')
print('4. Pound to Ton')
print('5. Ton to Pound')
print('6. Ton to Ounce')
print('7. Ton to Kilograms')
print('8. Quit')
main()
Upvotes: 0
Views: 226
Reputation: 9472
You cannot have a function name which contains a space , replace it with an underscore _
, it should work.
See Python PEP 8. The rules for naming variables have to followed strictly.
1) Function names should be lowercase, with words separated by underscores and not spaces as necessary to improve readability.
2) mixedCase is allowed only in contexts where that's already the prevailing style Variables...
So your updated code will look like this:
#This program allow for the user to convert weight MANAGEMENTS
#Import Modules
import KilogramConversion
import PoundConversion
import TonConversion
#Menu choice constants
CONVERT_KILOGRAMS_OUNCE=1
CONVERT_KILOGRAMS_POUND=2
CONVERT_POUND_OUNCE=3
CONVERT_POUND_TON=4
CONVERT_TON_POUND=5
CONVERT_TON_OUNCE=6
CONVERT_TON_KILOGRAMS=7
QUIT_CHOICE=8
# Main Function
def main():
choice=0
while choice != QUIT_CHOICE:
display_menu()
choice=int(input('Enter your choice:'))
#Perform menu choice
if choice == CONVERT_KILOGRAMS_OUNCE:
kilo=float(input("Enter weight in Kilograms:"))
print('The weight in ounces is:', KilogramConversion.ounce(kilo))
elif choice == CONVERT_KILOGRAMS_POUND:
kilo=float(input("Enter weight in Kilograms:"))
print('The weight in pounds is:', KilogramConversion.pound(kilo))
elif choice == CONVERT_POUND_OUNCE:
lbs=float(input("Enter weight in Pounds:"))
print('The weight in ounces is:', PoundConversion.ounce(lbs))
elif choice == CONVERT_POUND_TON:
lbs=float(input("Enter weight in Pounds:"))
print('The weight in tons is:', PoundConversion.ton(lbs))
elif choice == CONVERT_TON_POUND:
tons= float(input("Enter weight in Tons:"))
print('The weight in pounds is:', TonConversion.pound(tons))
elif choice == CONVERT_TON_OUNCE:
tons= float(input("Enter weight in Tons:"))
print('The weight in ounces is:', TonConversion.ounce(tons))
elif choice == CONVERT_TON_KILOGRAMS:
tons= float(input("Enter weight in Tons:"))
print('The weight in kilograms is:', TonConversion.kilogram(tons))
elif choice == QUIT_CHOICE:
print('Exiting...')
else:
print('Invalid Selection')
def display_menu():
print(' ')
print(' ')
print(' Menu')
print('1. Kilograms to Ounce')
print('2. Kilograms to Pound')
print('3. Pound to Ounce')
print('4. Pound to Ton')
print('5. Ton to Pound')
print('6. Ton to Ounce')
print('7. Ton to Kilograms')
print('8. Quit')
main()
Upvotes: 1
Reputation: 40743
Line 24: you called display menu()
instead of display_menu()
. Also, you need to define display_menu()
before calling it.
Upvotes: 0