否则,说明未正确遵循elif

2024-09-27 17:56:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我被指派制作一个程序,接受用户输入(温度),如果温度是摄氏度,则转换成华氏度,反之亦然。你知道吗

问题是,当您键入类似于35:C的内容时,程序使用if myscale==“F”而不是elif myscale==“C”,即使myscale是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")

Tags: ortoininfoifquitprintenter
2条回答

你的问题是:

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

注意:后面的elif

你也应该使用in,正如其他答案所指出的那样。你知道吗

以下内容:

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

应为:

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

或者

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

或者(如果您的Python足够新,可以支持set文本):

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

同样的道理

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

此外,在elif后面还有一个外来结肠。你知道吗

你现在所拥有的在语法上是有效的,但是做了一些不同于预期的事情。你知道吗

相关问题 更多 >

    热门问题