(Python)试图让"if not in"工作,它正在搜索一个冷冻集

rai*_*vbs 4 python if-statement

这是我的代码,正如我所说,我试图这样做,如果他们为section选项输入的内容不在任何一个frozensets中,它打印我所拥有的然后重新启动程序.

import sys
import os

temp = float(input('Please enter the temperature (In Celsius or Fahrenheit): '))
unit = str(input('Now, is this in Fahrenheit(F) or Celsius(C)? '))

Fahrenheit = frozenset(["F","f","Fahrenheit","fahrenheit","Fah","fah"])
Celsius = frozenset(["C","c","Celsius","celsius","Cel","cel"])
FahandCel = Fahrenheit & Celsius

if unit in Fahrenheit:
    answerC = (temp-32)*5/9
    print('\nYour original temperature of {}F is {}C'.format(temp,answerC))

if unit in Celsius:
    answerF = temp*9/5+32
    print('\nYour original temperature of {}C is {}F'.format(temp,answerF))

if unit not in FahandCel:
    print('\nPlease actually enter something obvious next time.\nSuch as, F, C, Fahrenheit, or Celsius.\n\n')
    python = sys.executable
    os.execl(python, python, * sys.argv)
Run Code Online (Sandbox Code Playgroud)

当我启动程序时,不管我输入的是第二个输入,它都打印出"请实际输入..."的行.即使我输入的是华氏温度或摄氏温度.

Ign*_*ams 5

frozenset.__and__()设置交集,留下一个空集.

但是,您不可能在两个集合中具有相同的单元,因此您的代码应如下所示,从而完全不需要集合联合:

if unit in Fahrenheit:
   ...
elif unit in Celsius:
   ...
else:
   ...
Run Code Online (Sandbox Code Playgroud)

除了你应该使用FAHRENHEITCELSIUS因为PEP 8.