Question:
Programming, please help!?
queenbee
2009-02-25 14:32:55 UTC
im writing a program for an assignment but im stuck, i have 5 conversion options to choose from and each option had the option of "a" or "b", the problem is i dont know how to define the "a" and "b"
here is an example of what im doing


def print_menu():
print " 'p' Print Menu"
print " 's' Speed"
print " 'v' Volume"
print " 'c' Currency"
print " 'w' Weight"
print " 't' Temperature"
print " 'q' Quit the Program"
print

def miles_to_kilometers(miles):
return miles*1.6

def kilometers_to_miles(kilometers):
return kilometers*0.62

def pints_to_litres(pints):
return pints*0.47

def litres_to_pints(litres):
return litres*2.1

def euro_to_dollar(euro):
return euro*1.46

def dollar_to_euro(dollar):
return dollar*0.79

def kg_to_stone(kg):
return kg*0.15

def stone_to_kg(stone):
return stone*6.3

def celsius_to_fahrenheit(celsius):
return celsius*9/5+32

def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit-32)/1.8

print_menu()
choice = raw_input("Which conversion do you wish to preform?")

while choice !="q":

if choice == "p":
choice =raw_input("Which conversion do you wish to preform?")

elif choice == "s":
speed = input("Do you want to convert miles to kilometers (a) or kilometers to miles (b)?.")
if speed == "a":
miles = input("Please enter miles to be converted.")
print miles,"Miles is",miles_to_kilometers(miles),"Kilomete...
elif choice == "b":
kilometers=input("Please enter kilometers to be converted.")
print kilometers,"Kilometers is",kilometers_to_miles(kilometers),"Mil...
choice =raw_input("Which conversion do you wish to preform?")

and here is the error message i get when i try to run it:

Traceback (most recent call last):
File "G:\IT folder\programming\asigment2copy.py", line 53, in
speed = input("Do you want to convert miles to kilometers (a) or kilometers to miles (b)?.")
File "", line 1, in
NameError: name 'a' is not defined

please help if you can thanks
Three answers:
guanotwozero
2009-02-25 16:26:49 UTC
I don't know that language - (python?), but I'm guessing that "(a)" in an input string may be a way of placing variable values, so try using a different way of asking for a or b



I also think you'll need to use inner and outer conditionals for the input. In pseudocode:



outerChoice = getInput("Enter menu choice")

while (outerChoice != "q") {

__if (outerChoice == "s") {

____innerChoice = getInput("Enter a or b")

____if (innerChoice == "a") {

______// do conversion a

____}

____else {

______// do conversion b

____}

__}

__else if (outerChoice == "v") {

____// do it all for v

__}

__// yadda yadda for rest of outer menu

}
?
2016-10-20 02:03:08 UTC
software has develop into the conventional for computing. Programme is the unique British spelling - the two could be utilized by utilising individuals - different than that bill Gates' spellchecker will provide up you.
Sparky the spark
2009-02-25 14:38:46 UTC
sounds stupid but aren't you missing a def "a" line out in your definition section?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...