1

This is my project description:

Write a program that displays the following menu:

1) Convert Fahrenheit to Celsius
2) Convert Celsius to Fahrenheit

Enter 1, 2, or 0 to exit:

In the case of options 1 and 2, the program should then prompt the user for the temperature, perform the conversion, and output the result. Then the program should re-display the menu. When option 0 is chosen, the program should exit.

Here is what I have so far:

a = raw_input("1) Convert Fahrenheit to Celsius \n2) Convert Celsius to Fahrenheit \nEnter 1, 2, or 0 to exit: ")

x = raw_input("Enter degree: ")

y = float((x - 32) / 1.8)

x = raw_input("Enter degree: ")

z = float((x + 32) * 1.8)

if a == 1:
    print(y)

if a == 2:
    print(z)

How can I terminate this program? Where else am I messing up?

2
  • Homework? If so, you should tag it as such. Commented Sep 10, 2011 at 5:33
  • 1
    I'd feel like a jerk if I didn't point out that your z equation should read: z = float((x / 1.8) + 32) Commented Sep 10, 2011 at 16:01

2 Answers 2

1
  • Get rid of the second x = raw_input(... line. You don't need it.
  • Calculate y right before you print it, inside the if block. No point doing math the user didn't want.
  • Same for z: calculate it right before you need it.
  • Put this after the a = raw_input(... line:

Code:

if a == 0:
    import sys
    sys.exit(0)
Sign up to request clarification or add additional context in comments.

1 Comment

Just an addition: No one has mentionned the loop so far. Of course all that has to be in a while True: loop. And then you also can use break to leave that loop and terminate.
0

You need to:

  1. convert a and x to int.

    a = int(raw_input("1) Convert Fahrenheit to Celsius \n2) Convert Celsius to Fahrenheit \nEnter 1, 2, or 0 to exit: "))
    
    x = int(raw_input("Enter degree: "))
    
  2. Do the calculations inside if blocks.

  3. Also, get rid of the second x = raw_input("Enter degree: ")

  4. And as said in above answer, you can use the sys.exit(0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.