0

I was given the assignment to make a program that takes user input (a temperature) and if the temperature is Celsius convert to Fahrenheit and Vice versa.

The problem is that when you type something like 35:C the program uses the if myscale == "F" instead of the elif myscale == "C" even though myscale is C my code:

mytemp = 0.0
while mytemp != "quit":
    info = raw_input("Please enter a temperature and a scale. For example - 75:F " \
                       "for 75 degrees farenheit or 63:C for 63 degrees celcius "\
                       "celcious. ").split(":")
    mytemp = info[0]
    myscale = str(info[1])

    if mytemp == "quit":
        "You have entered quit: "
    else:
        mytemp = float(mytemp)
        scale = myscale
        if myscale == "f" or "F":
            newtemp = round((5.0/9.0*(mytemp-32)),3)
            print "\n",mytemp,"degrees in farenheit is equal to",newtemp,"degrees in 
            celcius. \n" 
        elif: myscale == "c" or "C":
            newtemp = 9.0/5.0*mytemp+32
            print "\n",mytemp,"degrees in celcius is equal to",newtemp,"degrees in 
            farenheit. \n"
        else:
            print "There seems to have been an error; remember to place a colon (:) 
                  between "\
                  "The degrees and the letter representing the scale enter code here. "
raw_input("Press enter to exit")
donsavage
  • 387
  • 2
  • 4
  • 11

2 Answers2

2

The following:

    if myscale == "f" or "F":

should read:

    if myscale == "f" or myscale == "F":

or

    if myscale in ("f", "F"):

or (if your Python is recent enough to support set literals):

    if myscale in {"f", "F"}:

The same goes for

    elif: myscale == "c" or "C":

Also, there is an extraneous colon after the elif.

What you have now is syntactically valid but does something different to what is intended.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Specifically, the condition is parsed the same as `(myscale=="f") or "F"`, and `"F"`--being a non-empty string--will always evaluate true. – chepner Mar 11 '13 at 21:30
0

Here is your problem:

elif: myscale == "c" or "C":

Note the : after the elif

You also should use in as noted by the other answers.

N_A
  • 19,799
  • 4
  • 52
  • 98