0

I'm writing a simple algorithm to convert between degrees Celsius and degrees Fahrenheit the rest of the algorithm works perfectly so long as input for tu is limited to either "F" or "C". however I want to actually prevent users from entering anything but those strings.

I want to replace the commented area with something that will trigger the error message and sys.exit()

import sys
t = 0.0
tu = "null"

print "This program will convert between degrees C and F." 

print "Input temperature unit. (F or C)"
tu = raw_input()
#if (tu != "F" and != "C"):
    #print "The directions were clear, try again."
    #sys.exit()


Even input that should be allowed triggers the sys.exit(). I obviously don’t want that.

Tim Hill
  • 3
  • 1

1 Answers1

0

Your condition has an error in it, it should be

if tu != "F" and tu != "C":

To check that a variable is one of multiple values you can use in

if tu not in ('F', 'C'):
Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50