为了账号安全,请及时绑定邮箱和手机立即绑定

未定义/未使用的变量或意外缩进

未定义/未使用的变量或意外缩进

慕标琳琳 2023-04-18 17:32:08
我不断得到这两个。如果我设法修复一个,另一个就会出现。这将得到引号的未定义/未使用的变量错误。import randomdef primary():  print("Keep it logically awesome.")   f = open("quotes.txt")  quotes = f.readlines()  f.close()last = len (quotes) - 1rnd = random.randint(0, last)  print(quotes[rnd])if __name__== "__main__":primary()如果我简单地从中删除所有空格,print (quotes[rnd])我会得到“意外缩进”
查看完整描述

3 回答

?
慕的地10843

TA贡献1785条经验 获得超8个赞

正确的代码如下所示。


你必须在几个地方意图。


import random

def primary():

    print("Keep it logically awesome.")


    f = open("quotes.txt")

    quotes = f.readlines()

    f.close()


    last = len (quotes) - 1  #this shud be intented

    rnd = random.randint(0, last) #this shud also be intented


    print(quotes[rnd]) #this shud be intented


if __name__== "__main__":

    primary() #if statements shud always be intented

编写单行 if 语句的另一种方法是


if __name__== "__main__": primary()

查看完整回答
反对 回复 2023-04-18
?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

import random

def primary():

    print("Keep it logically awesome.")

 

    f = open("quotes.txt")

    quotes = f.readlines()

    f.close()


    last = len(quotes) - 1

    rnd = random.randint(0, last)

    print(quotes[rnd])


if __name__== "__main__":

    primary()

希望这对您有所帮助。享受!


查看完整回答
反对 回复 2023-04-18
?
RISEBY

TA贡献1856条经验 获得超5个赞

import random


def primary():

    print("Keep it logically awesome.")


    f = open("quotes.txt")

    quotes = f.readlines()

    f.close()


    last = len(quotes) - 1

    rnd = random.randint(0, last)


    print(quotes[rnd])


if __name__== "__main__":

  primary()


查看完整回答
反对 回复 2023-04-18
  • 3 回答
  • 0 关注
  • 113 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信