I'm still grappling with the concept of passing parameters and returning responses. See code below:

#Measurement Converter
# Converts Common Chinese Units of Measurements (Practicing function return)

# Convert "cun" to "inches"
def cun_to_inches(cuntoinchesQ):
    cuntoinchesUR = None
    while cuntoinchesUR == None:
        cuntoinchesUR = float(raw_input(cuntoinchesQ))
        cuntoinchesUR *= 1.31
    return cuntoinchesUR

# convert "inches" to "cun"
def inches_to_cun(inchestocunUR1):
    inchestocunUR2 = inchestocunUR1
    inchestocunUR1 *= 0.76
    print inchestocunUR2, "inches is", inchestocunUR1, "cun."

[B]def cun_to_centimeters(cuntocentimetersUR1):
    cuntocentimetersUR2 = cuntocentimetersUR1
    cuntocentimetersUR1 *= 3.33
    return cuntocentimetersUR2, cuntocentimetersUR1[/B]

userchoice = None
while userchoice != "q":
    print """ \
            CHINESE UNIT OF MEASUREMENT CONVERTER v1.0
        Convert common units of measurement used in China into
             Western / American measurements equivalents.

    Choose your conversion type:

    1 - Cun to inches
    2 - Inches to cun
    3 - Cun to centimeters
    4 - Centimeters to cun
    5 - Kilojules to calories
    6 - Calories to kilojules
    7 - Feet to centimeters
    8 - Centimeters to feet
    9 - Jin to pounds
    10 - Pounds to jin
    11 - Mu to acres
    12 - Acres to mu

    PRESS Q TO QUIT
    """
    userchoice = int(raw_input("Please choose conversion type: ").lower())
    if userchoice == 1:
        cuntoinchesA = cun_to_inches("\nPlease enter the number of cun: ")
        print "That many cun equals ", cuntoinchesA, "inches."
    elif userchoice == 2:
        inchestocunA = inches_to_cun(float(raw_input("Please enter the number of inches: ")))
   [B] elif userchoice == 3:
        cuntocentimetersUR2 = cun_to_centimeters(float(raw_input("Please enter the number of cun: ")))
        print cuntocentimetersUR2, "cun is", cuntocentimetersUR1, "centimeters."[/B]
    else:
        print "That is not a valid response."

raw_input("See ya! Press Enter to close.")

In cun_to_inches() and inches_to_cun(), I understand how that works. But in cun_to_centimeters(), I can't figure out how to pass multiple responses back to the main program.

Any help at all would be appreciated. Wordy explanations welcome. I always figure I definitely understand something when I know all the possible ways to use it.

Recommended Answers

All 2 Replies

You are returning a tuple in cun_to_centimeters(). Access the elements of the tuple by slicing.
Example:

>>> def ret_tuple():
... 	return 1, 2
... 
>>> obj = ret_tuple()
>>> obj[0]
1
>>> obj[1]
2
>>>

Ah HAH. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.