0

I have to make a program that asks for the "login hour" 24 times, puts all those values into a list and then outputs how many times each login hour was used and if you type -1 it breaks the loop for example with 6 inputs it would be.

Please enter login hour: 3
Please enter login hour: 4
Please enter login hour: 3
Please enter login hour: 3
Please enter login hour: 4
Please enter login hour: -1
There were 0 logins In hour 0
There were 0 logins In hour 1
There were 0 logins In hour 2 
There were 3 logins In hour 3
There were 2 logins In hour 4
....till 24

This is what I have done so far. I just do not know how to count each element type and make them separate from the rest.

loginCount = 0
hour = []
for i in range(0,24):
     item = int(input("please enter login hour:")
     hour.append(item)
     if hour[i] == -1
          break
     else: 
          loginCount = loginCount + 1

3 Answers 3

1

You can use a fixed size array and up count appropriate index each time: It is hour[item-1] since list indexing starts at 0.

hour = [0] * 24
for i in range(0,24):
   item = int(input("please enter login hour: "))
   if item == -1:
      break
   else:
      hour[item] += 1
Sign up to request clarification or add additional context in comments.

Comments

0
# We will maintain a list with 24 entries
# Each entry in the list will maintain a count for the number
# of logins in that hour. Finally, we will print all the entries
# from the list that will output number of logins for each hour

# Creating a list with one entry for each hour
records = [0] * 24

# Asking user 24 times
for j in range(0, 24):
    i = int(input("please enter login hour:"))
    # Since lists are mutable, we can increment the count 
    # at the corresponding position of the input hour
    if i != -1:
        records[i - 1] = records[i - 1] + 1
    else:
        break

# Print all the entries from the records
for i in range(len(records)):
    print("There were " + str(records[i]) + " logins in hour " + str(i+1))

Comments

0

I have added a while loop to count the number of elements in the list. Hope you do not want to add -1 to the list. Included if statement to not append -1 and break when user inputs -1 Before the second loop, I have created another list which will contain unique values from hour to print number of attempts for only those numbers for whom attempt has actually been made(this is slightly different from what you have mentioned in question, You can get that be changing sorted(unique_hrs) in second loop to range(1,25)).

hour = []
while len(hour) < 24:
    item = int(input("please enter login hour: "))
    if item != -1:
        hour.append(item)
    else:
        break

unique_hrs = list(set(hour))
for i in sorted(unique_hrs):
    print('There were ' + str(hour.count(i)) + ' logins In hour ' + str(i))

1 Comment

It was my bad. I did not realize it wont help OP. I just copied and pasted the working code without explaining what I am doing here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.