python - 如何在 python 函数之外使用递增变量?
问题描述
这可能是一个愚蠢的问题;也许我的过程完全错误(如果是,请指出),但是如何在自定义函数中提取三个递增变量(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 += 1
length_finder(input("Enter the text you wish to analyze: "))
L = 100/c_word*c_char
S = 100/c_word*c_sentence
#formula to get readability
index = 0.0588 * L - 0.296 * S - 15.8
print("This text is suitable for grade " + str(index))
解决方案
您可以return
在函数中使用多个变量:
def length_finder(x):
...
return (c_char, c_word, c_sentence)
(c_char, c_word, c_sentence) = length_finder('input string')
推荐阅读
- javascript - AMP 设置和获取 cookie 值
- android - 错误:ADB 以退出代码 1 退出正在执行流式安装
- apache-kafka - 带有 kerberos 的 kafka-console-consumer 抛出没有枚举常量异常
- javascript - 如何使用 jquery 设置和重新设置 dropify 默认图像?
- python - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 51-52: malformed \N character escape
- javascript - 获取价值数据并根据每个环节变化?
- javascript - javascript 中的实用程序映射器函数
- amazon-web-services - 使用 SES 发送带有多个附件的电子邮件
- reactjs - 成功“提交”后 ReduxForm 重置表单
- python - Spark Python - 从结构中选择并从特定深度返回