亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

未定義/未使用的變量或意外縮進

未定義/未使用的變量或意外縮進

慕標琳琳 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 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號