分类:字符串
知识点:
-
获取输入 input().strip().split(" ")
-
拼接列表 " ".join(list)
-
输出指定位数的浮点数 print("%.2f" % value)
-
len()
函数对于很多内置的数据类型都适用,它返回对象的元素个数或长度。
# 用法1: 获取字符串的长度
string_example = "Hello, World!"
length_of_string = len(string_example)
print("字符串的长度为:", length_of_string)
# 用法2: 获取列表或元组的元素个数
list_example = [1, 2, 3, 4, 5]
length_of_list = len(list_example)
print("列表的元素个数为:", length_of_list)
# 用法3: 获取字典的键值对个数
dictionary_example = {"a": 1, "b": 2, "c": 3}
length_of_dictionary = len(dictionary_example)
print("字典的键值对个数为:", length_of_dictionary)
# 用法4: 获取集合的元素个数
set_example = {1, 2, 3, 4, 5}
length_of_set = len(set_example)
print("集合的元素个数为:", length_of_set)
# 用法5: 获取其他可迭代对象的元素个数
tuple_example = (1, 2, 3, 4, 5)
length_of_tuple = len(tuple_example)
print("元组的元素个数为:", length_of_tuple)
题目来自【华为招聘模拟考试】
# If you need to import additional packages or classes, please import here.
def func():
# please define the python3 input here. For example: a,b = map(int, input().strip().split())
words = input().strip().split(" ")
# please finish the function body here.
total_words = len(words)
new_str = "".join(words)
total_length = len(new_str)
# please define the python3 output here. For example: print().
# print(round(total_length/total_words, 2)) # round有bug
print("%.2f" % (total_length/total_words))
if __name__ == "__main__":
func()
注意,不要使用round,有bug
by: 软件工程小施同学