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

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

如何在 python 函數之外使用遞增變量?

如何在 python 函數之外使用遞增變量?

Go
慕婉清6462132 2022-10-06 19:34:57
這可能是一個愚蠢的問題;也許我的過程完全錯誤(如果是,請指出),但是如何在自定義函數中提取三個遞增變量(c_char、c_word、c_sentence)并將其用于其他用途?def length_finder(x):    #variables counting character,word,sentnece      c_char = 0    c_word = 1    c_sentence = 0    for i in x:        if (i >= 'a' and i <= 'z') or (i >= 'A' and i <= 'Z'):            c_char += 1        if i == " ":            c_word += 1        if i == '.' or i == '!' or i == '?' :            c_sentence += 1length_finder(input("Enter the text you wish to analyze: "))L = 100/c_word*c_charS = 100/c_word*c_sentence#formula to get readabilityindex = 0.0588 * L - 0.296 * S - 15.8print("This text is suitable for grade " + str(index))
查看完整描述

2 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

您可以return在函數中使用多個變量:


def length_finder(x):

    ...

    return (c_char, c_word, c_sentence)



(c_char, c_word, c_sentence) = length_finder('input string')


查看完整回答
反對 回復 2022-10-06
?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

你有兩個選擇。

  1. 將這三個變量設為全局變量,并在函數中引用它們。您可以將它們設為全局,然后在函數中將它們作為全局引用。

  2. 從函數中返回它們。您可以將所有 3 個值作為字典或列表返回。


查看完整回答
反對 回復 2022-10-06
  • 2 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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