def的语法错误

时间:2014-05-18 21:30:08

标签: python python-2.7 syntax function

我正在设计一个程序来测试你输入随机引号的速度。我一直试图运行它,但它不断给我一个语法错误并突出显示 def 命令。我试图篡改缩进,认为这是问题所在。以下是所有代码:

import time, sys, random
random = ["Behind every great man is a woman rolling her eyes.", "A day without  sunshine is like, you know, night.", "If the facts don't fit the theory, change the facts.","Housework can't kill you, but why take a chance?", "Weather forecast for tonight: dark."
def main():          
 ready = raw_input("Are you ready to begin?")
 if ready == 'yes' or 'y':
    print "3..."
    time.sleep(1)
    print "2..."
    time.sleep(1)
    print "1..."
    time.sleep(1)
    now = time.time()
    quote = random.choice(random)
    print quote
    enter = raw_input()
    future = time.time()
    time = future - now
    if enter == quote:          
        correct = "You had no mistakes."
    else:
        correct = "You had mistake(s)."
    quote2 = len(quote)
    wordlen = quote2 / 5
    print "Results:"
    print "The quote was "+quote2+" letters long and approx. "+wordlen+" words long."+correct+"Good job!"

if ready == 'no' or 'n':
    print "Take your time."
    sys.exit()
else:
    print "What was that?"
    main()

2 个答案:

答案 0 :(得分:4)

您没有关闭列表:

random = ["Behind every great man is a woman rolling her eyes.", "A day without  sunshine is like, you know, night.", "If the facts don't fit the theory, change the facts.","Housework can't kill you, but why take a chance?", "Weather forecast for tonight: dark."]

答案 1 :(得分:1)

您的其他错误来自使用time = future - now

不要将模块名称用作变量名称,您也使用random作为列表名称。

time = future - now更改为time_diff = future - now

random更改为您的列表名称,以及random_list更改代码中的内容。

请注意,if语句总是会被评估,因此如果您有两个条件要检查使用if/elif,那么您的语句顺序就会很重要,就好像第一个if是{{1}一样你不需要评估第二个语句。仅当第一个True语句为False时,才会评估elif语句。

此代码可以使用:

if