Python Forum

Full Version: Quick Lists Question (Count Occurences)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good Evening,

I'm stuck in a headache and am looking to get some solutions. I've been given a problem that wants me to input 9 integers, and list the occurrences of those 9 integers. I've done it one way but the tool our class uses called 'Livelab' doesn't accept the code I gave it. For this purpose I will show both source code.


For the rejected code I used: 

from collections import Counter
a = [2, 5, 6, 5, 4, 3, 23, 43, 2]
print(Counter(a))
The code I'm working on currently looks like this. I've got some of the print statements to work within Idle, but am having trouble and the book I have has been little help.


for i in range(9):
   num_list = int(input("Enter integers between 1 and 100: "))
print(num_list.count(2))
print(num_list.count(5))
print(num_list.count(6))
print(num_list.count(4))
print(num_list.count(3))
print(num_list.count(23))
print(num_list.count(43))
Any help would be appreciated!
On the numbers:
input all nine with one input statement:
my_numbers = input('Enter 9 numbers separated by spaces')
mn = my_numbers.split(' ')
if len(mn) <> 9):
    # Show error Do it again
Don't put that in a loop
That isn't in python 3, or at least my interpret is saying it isn't. I'm talking about the <>.

I messed with that code a little bit though. That's not what I'm looking to do. I'm looking to input 9 numbers and after that count how many times each number occurs within that list.
(Nov-16-2016, 12:17 AM)Larz60+ Wrote: [ -> ]if len(mn) <> 9):
You should use !=

<> is old python, and doesnt work in python3.x
(Nov-16-2016, 12:28 AM)metulburr Wrote: [ -> ]
(Nov-16-2016, 12:17 AM)Larz60+ Wrote: [ -> ]if len(mn) <> 9):
You should use !=

<> is old python, and doesnt work in python3.x
Per PEP 401, that syntax is valid as of 3.1, as long as you 
from __future__ import barry_as_FLUFL


...and don't mind April fools modules...
i copied this thread to split the talk of <> into a new thread.
does anyone here use 3.1? does anyone over there use 3.1?
Sorry old habit
(Nov-16-2016, 03:01 AM)Skaperen Wrote: [ -> ]does anyone here use 3.1?  does anyone over there use 3.1?

I use 3.5.2.
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import barry_as_FLUFL
>>> True != False
  File "<stdin>", line 1
    True != False
          ^
SyntaxError: invalid syntax
>>> True <> False
True
Actually the snippet of code that I used it in, was never tested, I wrote it on the fly.
I used <> because it's something I did for years while programming in C and C++
I also use python 3.5.2, and may have been using it without thinking.