Python语法错误与功能

时间:2016-08-30 18:05:07

标签: python syntax

我的python代码由于某种原因不能工作。它说错误来自函数的语法,但我不确定它为什么这样做

one=1
two=2
three=3
four=4
five=5
six=6
seven=7
eight=8
nine=9
ten=10
print "test"
def convert()
  number = raw_input('Enter the number you need converted to binary')
enterYourCommand = raw_input("Enter your command")
if enterYourCommand is "convert"
  convert()
elif enterYourCommand is "tonumber"
  tonumber()

3 个答案:

答案 0 :(得分:1)

在函数定义之后没有:,如果是:

one=1
two=2
three=3
four=4
five=5
six=6
seven=7
eight=8
nine=9
ten=10
print "test"

def convert():
  number = raw_input('Enter the number you need converted to binary')

enterYourCommand = raw_input("Enter your command")

if enterYourCommand is "convert":
  convert()
elif enterYourCommand is "tonumber":
  tonumber()

答案 1 :(得分:0)

您在ifelifdef之后错过了冒号。你必须缩进四个空格。有关示例,请参阅this link

one = 1
two = 2
three = 3
four = 4
five = 5
six = 6
seven = 7
eight = 8
nine = 9
ten = 10

enterYourCommand = raw_input("Enter your command")

def convert():
    number = raw_input('Enter the number you need converted to binary')

if enterYourCommand == "convert":
    convert()
elif enterYourCommand == "tonumber":
    tonumber()

希望它有所帮助。

编辑

is替换为==

    如果两个变量指向同一个对象,
  • is将返回True
  • 如果变量引用的对象相等,
  • ==将返回True

资料来源:is-there-a-difference-between-and-is-in-python

答案 2 :(得分:0)

所有python函数的声明行末尾都应该有一个冒号:

例如:

    def convert():
        number = raw_input('Enter the number you need converted to binary')

此外,您的ifelif声明也是如此:

    if enterYourCommand is "convert":
        convert()
    elif enterYourCommand is "tonumber":
        tonumber()

所以,只需在每个声明的末尾添加:,你就应该好了。