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



我一直得到这两个。如果我设法修复了一个,另一个就会出现。

这将导致引号出现未定义/未使用的变量错误。

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()

如果我简单地从CCD_ 1移除所有空间;意外缩进";

正确的代码如下所示。

你必须在几个地方下决心。

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 __name__== "__main__": primary()

有关缩进的更多信息,请参阅:https://docs.python.org/3/reference/lexical_analysis.html#indentation

https://www.geeksforgeeks.org/indentation-in-python/amp/

https://www.w3schools.com/python/gloss_python_indentation.asp

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()

希望这对你有帮助。享受

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()

最新更新