Python中意外的返回变量

时间:2018-09-06 20:22:01

标签: python string function

我正在使用Python,并且几乎已经编写了一些代码作为必需作业的一部分。

#------Unit converter------#
#-------Challenge 5-------#
#-------Definitions-------#
def currencyConvert():
    print("C")

def tempConvert():
    print("T")

def massConvert():
    print("M")

def volConvert():
    print("V")

#-------Executions--------#
print("Welcome to my unit converter.\nIn this software, you can convert between the following things:")
print(" - Currency: United States Dollar (USD), Pound Sterling (GBP) and Serbian Dinar (RDS).")
print(" - Temperature: Fahrenheit (F) and Celsius (C)")
print(" - Mass: Pounds (lbs) and grams (mg/g/kg)")
print(" - Volume: Fluid ounces (fl oz) and litres (ml/cl/l")

def phaseOne():
    global unitType
    print("--------------------")
    unitType = input("What kind of conversion would you like to make? Currency (C), temperature (T), mass (M) or volume (V)?\n>>>")
    if unitType == "C" or "c":
        currencyConvert()
    elif unitType == "T" or "t":
        tempConvert()
    elif unitType == "M" or "m":
        massConvert()
    elif unitType == "V" or "v":
        volConvert()
    else:
        print("That is not a valid input. Please try again.")
        phaseOne()        

phaseOne()

我的问题是,当我运行程序并输入“ V”时,我希望调用volConvert()函数(因此输出“ V”),但是它返回“ C”。是什么原因造成的?

我还没有Python的技能,但是我正在研究它,并且需要一些小的帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

您作为(unitType == "C") or "c"工作的条件不是您想要的。相反,您需要unittype in ("C", "c")