【问题标题】:Syntax error with def [closed]def 语法错误[关闭]
【发布时间】:2014-05-18 21:30:08
【问题描述】:

我正在设计一个程序来测试您输入随机引号的速度。我一直在尝试运行它,但它一直给我一个语法错误并突出显示 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()

【问题讨论】:

  • 阅读:显示的错误已修复,但现在它在 ln 7 上显示“UnboundLocalError: local variable 'time' referenced before assignment”。
  • 您的评论应该是一个新问题,单独发布。现在您在这一个问题下有两个完全不同的问题的答案。

标签: python python-2.7 syntax function


【解决方案1】:

你没有关闭你的列表:

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."]

【讨论】:

  • 哎呀。完全错过了。 T
  • 非常感谢(在最后一条评论中不小心按了回车键)
【解决方案2】:

您的另一个错误来自使用time = future - now

不要使用模块名作为变量名,你也使用random作为你的列表名。

time = future - now 更改为time_diff = future - now 之类的东西

random 作为您的列表名称更改为类似 random_list 在您的代码中更改它。

请记住,if 语句始终会被评估,因此语句的顺序很重要,如果您有两个条件要检查使用 if/elif,就好像第一个 ifTrue 您不需要评估第二个陈述。仅当第一个 if 语句为 False 时才评估 elif 语句。

此代码将起作用:

import  time,sys, random
# changed name from random to random_list
random_list = ["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 ready =='y': # need to use or ready =='y' not or == "y"
        print "3..."
        time.sleep(1)
        print "2..."
        time.sleep(1)
        print "1..."
        time.sleep(1)
        now = time.time()
        quote = random.choice(random_list)
        print quote
    elif ready == 'no' or ready =='n':# moved from bottom to here and changed to elif.
        print "Take your time."
        sys.exit()
    else:
        print "What was that?"
        main()
    enter = raw_input("Enter the quote: ")
    future = time.time()
    time_diff = future - now # changed name to time_diff instead of time
    if enter == quote:
        correct = "You had no mistakes."
    else:
        correct = "You had mistake(s)."
    quote2 = len(quote)
    wordlen = quote2 / 5
    print "Results:"      # quote2 and wordlen are ints you need to cast as strings to join to another string.
    print "The quote was "+str(quote2)+" letters long and approx. "+str(wordlen)+" words long."+correct+"Good job!"


main()

【讨论】:

  • 非常感谢!我还发现time_diff 没有指向任何东西:P 我现在有这个错误,当你完成时它会打印“慢慢来”。别担心,我会搞定的。
  • @user2352923,不客气。我已经编辑了代码,不完全确定你在做什么,但它应该让你更接近,你还有一些我在代码中评论过的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-19
  • 2014-08-25
  • 2018-09-23
  • 2014-02-12
  • 2012-08-05
  • 1970-01-01
相关资源
最近更新 更多