gpt4 book ai didi

python - 我如何计算输入的字符串的平均长度

转载 作者:太空宇宙 更新时间:2023-11-04 00:16:34 25 4
gpt4 key购买 nike

对于我的 python 作业,程序要求用户输入三个字符串并打印输入的字符串的平均长度

我的代码:

name1 = input ("enter first string: ")
name2 = input ("enter second string: ")
name3 = input ("enter third string: ")

total_strings = (name1.split()) + (name2.split()) + (name3.split())

average =( len(name1) + len(name2) + len(name3) )/len(total_strings)

print("the average length is: " + str(average))

所以当我尝试它时,数学变得有点奇怪。

输入:

a ajhs jah sjajsh jahs jah jah jsh as

ajs jahjah sjhajsh jahs jah sjah jsh ajs

ajs ajhs jah sjahs jah sjah sjah jsh ajhs jah s

=====这里测试失败=====

enter first string: enter second string: enter third string: the average length is: 4.428571428571429


预期输出:


enter first string: enter second string: enter third string: the average length is: 41.33

最佳答案

在 python 2.7 中完美运行。

name1 = raw_input("enter first string: ")
name2 = raw_input("enter second string: ")
name3 = raw_input("enter third string: ")

average = (len(name1) + len(name2) + len(name3)) / 3

print("the average length is: " + str(average))

希望对您有所帮助。

关于python - 我如何计算输入的字符串的平均长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50753321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com