if else statment不能正确遵循elif [英] if else statment not following the elif correctly

查看:115
本文介绍了if else statment不能正确遵循elif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被赋予了一个程序来制作一个程序,它接受用户输入(温度),如果温度是摄氏温度转换为华氏温度,反之亦然。

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.

问题是当你输入类似35:C的东西时,程序使用if myscale ==F而不是elif myscale ==C即使myscale是C我的代码:

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")


推荐答案

以下内容:

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

应为:

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

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

或(如果你的Python最近足以支持set literals):

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

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

同样适用于

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

此外,在 elif 之后还有一个无关的冒号。

Also, there is an extraneous colon after the elif.

你是什么现在已经在语法上有效,但做了与预期不同的事情。

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

这篇关于if else statment不能正确遵循elif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆