我的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()
答案 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)
您在if
,elif
和def
之后错过了冒号。你必须缩进四个空格。有关示例,请参阅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
。答案 2 :(得分:0)
所有python函数的声明行末尾都应该有一个冒号:
。
例如:
def convert():
number = raw_input('Enter the number you need converted to binary')
此外,您的if
和elif
声明也是如此:
if enterYourCommand is "convert":
convert()
elif enterYourCommand is "tonumber":
tonumber()
所以,只需在每个声明的末尾添加:
,你就应该好了。