如果Statement不适用于Python 2.7

时间:2015-04-27 15:35:08

标签: python python-2.7 if-statement calculator

我最近尝试用Python制作一个计算器,但我试图用if语句做的选项不起作用,如果你能请求告诉我有什么问题我会很感激,代码如下。是的我知道有错字我只是需要一些帮助,为什么这个if语句不起作用,因为我之前使用过它们并且它起作用

#Calculator Testing in Pytho 2.7

f == raw_input("""What function would you like to use?
You can type Opt to see the options"""

if f == "Opt":
    print
    """You can choose from the following options when at the 
    function selection screen;
    Type /Add for Addition
    Type /Sub for Subtraction
    Type /Div for Division
    Type /Multi for Multiplication

    Type /menu to go back to the function selection screen
    or
    Type /quit any time to end the program"""

if f == "no":
    print "test"



funcopt = raw_input("What would you to do?")

if funcopt == "/quit":
    print "test"

2 个答案:

答案 0 :(得分:7)

您的if声明有效。您的 print声明不起作用:

print
"""You can choose from the following options when at the 
function selection screen;
Type /Add for Addition
Type /Sub for Subtraction
Type /Div for Division
Type /Multi for Multiplication

Type /menu to go back to the function selection screen
or
Type /quit any time to end the program"""

该字符串定义是一个单独的行,而忽略了。 Python只是打印一个空行。

至少在同一行开始字符串:

print """You can choose from the following options when at the 
function selection screen;
Type /Add for Addition
Type /Sub for Subtraction
Type /Div for Division
Type /Multi for Multiplication

Type /menu to go back to the function selection screen
or
Type /quit any time to end the program"""

答案 1 :(得分:0)

你有一些代码制动语法错误。未经测试,但试试这个:

f = raw_input("""What function would you like to use?
You can type Opt to see the options""")

if f == "Opt":
    print """You can choose from the following options when at the 
    function selection screen;
    Type /Add for Addition
    Type /Sub for Subtraction
    Type /Div for Division
    Type /Multi for Multiplication

    Type /menu to go back to the function selection screen
    or
    Type /quit any time to end the program"""

if f == "no":
    print "test"

funcopt = raw_input("What would you to do?")

if funcopt == "/quit":
    print "test"

=为变量赋值,==比较值。 This可能有用