python - 如何让此代码对上面给出的名称进行计数和排序?(初学者)
问题描述
name = ["Safi", "Safi", "Safi", "Umaima", "Arsalah", "Farooq", "Ben", "Ben"]
list = ["Safi", "Safi", "Safi", "Umaima", "Arsalah", "Farooq", "Ben", "Ben"]
a = input("What would you like to know about the following list?")
if a == "Sort this list":
print (list.sort())
if a == "What is the Number of "+a+"'s":
print(list.count(+a))
else:
print("I don't understand")
该代码不想接受用户的输入,因为它只是跳过了用户的输入并且没有集成它。
解决方案
主要原因是 sort() 函数不返回任何内容,因此您必须在排序后打印列表:
name = ["Safi", "Safi", "Safi", "Umaima", "Arsalah", "Farooq", "Ben", "Ben"]
list = ["Safi", "Safi", "Safi", "Umaima", "Arsalah", "Farooq", "Ben", "Ben"]
a = input("What would you like to know about the following list?")
if a == "sort":
list.sort()
print (list)
if a == "count":
keyword = input("Which string to you want to count ?")
print (list.count(keyword)) #I think you want to count the occurence of the input word
推荐阅读
- c++ - GCC 11:如何告诉 CMake 我不想要默认的 C++ GNU 扩展?
- python - 有没有办法在没有服务器的情况下向客户端发送软件更新?
- php - 将 Laravel 项目复制到新设备后,PhpStorm 不会加载它们
- java - 如何编写一个方法(分解),将字符串作为输入并返回在java中组成字符串的字符数组?
- drop-down-menu - 如何让下拉菜单切换工作
- cmd - 在 proc 0 上出现错误:无法在.crack 中打开输入脚本:没有这样的文件或目录(src/lammps.cpp.494)
- python - 是否可以使用 Python 将所有域托管在网络服务器上?
- python - 如果未单击第一个图形,则不会加载第二个图形(Plotly Dash)
- javascript - 为什么我的 parseFloat 只允许将 1.00 到 999.00 的条目发回服务器
- python - 为什么每当我尝试将图像添加到我的 django 项目时都会出错?