2017-06-07 20 views
1

我试图使将输出多少次从一系列数字的数目7出现(无特定范围的)由输入的程序用户每个号码将是一个单独的输入,而不是一个。Python的3.x的来自用户的输入计数的特定数量的频率

我已搜查甚广,但解决方案,我发现涉及字母,单词或数字从预先制作的列表,而不是INT从用户输入和误当我试图修改的目的。我确信我错过了一些非常明显的事情,但我无法弄清楚如何做到这一点。

(我想反,如果num = = 100,计数(100),因为我在范围内,等等等等 - 但我清楚我在错误的道路上)

我的出发点是想修改该一个打印次数最多的,因为我的目标了类似的格式:

x = 0 
done = False 
while not done: 
    print("Enter a number (0 to end): ") 
    y = input() 
    num = int(y) 
    if num != 0: 
     if num > x: 
      x = num 
    else: 
     done = True 
print(str(x)) 

感谢您对这个有什么建议。

回答

0

尝试以下操作:

x = '' 
done = False 
while not done: 
    print("Enter a number (0 to end): ") 
    y = input() 
    if y != '0': 
     x = x + y 
    else: 
     done = True 

print(x.count('7')) 
+0

这很美!它没有行李就是我想要的。感谢您的时间和我头痛的解决方案! – CoderBaby

+0

乐意帮忙。您可以随时联系以获取其他帮助 –

0

您可以使用下面的代码示例。它期望第一个输入是您想要在列表中搜索的数字。随后在单独的一行中列出每个号码。

x = 0 
done = False 
count = 0 
i = input("Which number to search: ") 
print("Enter list of numbers to search number",i,", enter each on separate line and 0 to end): ") 
while not done: 
     j = input() 
     num = int(j) 
     if int(j) == 0 : 
       print("exitting") 
       break 
     else: 
       if j == i: 
         count += 1 
print("Found number",i,"for",count,"number of times") 
+0

不太我一直在寻找,但我会记住它为今后类似的事情。感谢您的时间和帮助! – CoderBaby

2

考虑

from collections import Counter 

nums = [] 
c = Counter() 
done = False 
while not done: 
    y = int(input("Enter a number (0 to end): ")) 
    if y == 0: 
     done = True 
    else: 
     c.update([y]) 
     print(c) 

输出示例:

Enter a number (0 to end): 1 
Counter({1: 1}) 
Enter a number (0 to end): 2 
Counter({1: 1, 2: 1}) 
Enter a number (0 to end): 2 
Counter({2: 2, 1: 1}) 
Enter a number (0 to end): 2 
Counter({2: 3, 1: 1}) 
Enter a number (0 to end): 0 

如果用户输入一个非整数这将明显破裂。如果需要,删除int(input..)或添加try-except

+0

不是我正在寻找的东西,而是一些代码,我会牢记以备将来参考。感谢您的时间和帮助! – CoderBaby