【发布时间】:2013-02-27 07:18:35
【问题描述】:
我需要帮助我编写一个函数,其中包括一个 while 循环,该循环将继续运行,直到用户输入一个空输入,一旦发生这种情况,该函数将返回输入名称的次数
到目前为止,我的代码是:
while True:
name = input('Enter a name:')
lst = name.split()
count={}
for n in lst:
if n in count:
count[n]+=1
for n in count:
if count[n] == 1:
print('There is {} student named {}'.format(count[n],\
n))
else:
print('There are {} students named {}'.format(count[n],\
n))
这里不再重复,只询问用户一次并返回 1
输出应如下所示:
Enter next name:Bob
Enter next name:Mike
Enter next name:Bob
Enter next name:Sam
Enter next name:Mike
Enter next name:Bob
Enter next name:
There is 1 student named Sam
There are 2 students named Mike
There are 3 students named Bob
【问题讨论】:
标签: python string python-3.x while-loop counter