-3

Suppose that you entered 3 5 2 5 5 5 0 and input always ends with the number 0, the program finds that the largest number is 5 and the occurrence count for 5 is 4.

Input: i enter 3 5 2 5 5 5 0 and it shows nothing as result

Code:

currentnum = int(input('Enter Numbers List, to end  enter 0: '))
maxx = 1
while currentnum > 0:
 if currentnum > maxx:
    max = currentnum
    count = 1
 elif currentnum == maxx:
  count += 1
print('The Largest number is:', int(maxx), 'and count is', int(count))
Michael M.
  • 10,486
  • 9
  • 18
  • 34
Fayakon
  • 1,173
  • 4
  • 23
  • 48
  • 1
    If I try the code as described, rather than "nothing happens", I get an error message. This is because the input `3 5 2 5 5 5 0` is not a valid integer. – Karl Knechtel Oct 02 '22 at 18:43

5 Answers5

4

Python doesn't have thunks.

currentnum = int(input('Enter Numbers List, to end  enter 0: '))

This sets currentnum to the result of running int on the value returned by the input call. From this point on, currentnum is an int.

while currentnum > 0:
 if currentnum > maxx:
    max = currentnum
    count = 1
 elif currentnum == maxx:
  count += 1

In this loop, you never take any more input, or reassign currentnum, so the loop will carry on forever, checking the same number over and over again.

If you assigned to currentnum at the end of your loop, you could take input in one-number-per-line. However, you want a space-separated input format, which can be better handled by iterating over the input:

numbers = [int(n) for n in input('Enter numbers list: ').split()]
max_num = max(numbers)
print(f"The largest number is {max_num} (occurs {numbers.count(max_num)} times)")

(Adding the 0-termination support is left as an exercise for the reader.)

Another, similar solution:

from collections import Counter

counts = Counter(map(int, input('Enter numbers list: ')))
max_num = max(counts, key=counts.get)
print(f"The largest number is {max_num} (occurs {counts[max_num]} times)")

I recommend trying your approach again, but using a for loop instead of a while loop.

wizzwizz4
  • 6,140
  • 2
  • 26
  • 62
0

Use the standard library instead. takewhile will find the "0" end sentinel and Counter will count the values found before that. Sort the counter and you'll find the largest member and its count.

import collections
import itertools

test = "3 5 2 5 5 5 0 other things"
counts = collections.Counter((int(a) for a in
        itertools.takewhile(lambda x: x != "0", test.split())))
maxval = sorted(counts)[-1]
maxcount = counts[maxval]
print(maxval, maxcount)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

Okay. So one of the most important things to do while programming is to think about the logic and dry run your code on paper before running it on the IDE. It gives you a clear understanding of what is exactly happening and what values variables hold after the execution of each line. The main problem with your code is at the input stage. When you enter a value larger than 0, the loop starts and never ends. To make you understand the logic clearly, I am posting a solution without using any built-in methods. Let me know if you face any issue

nums = []
currentnum = int(input('Enter Numbers List, to end  enter 0: '))
while currentnum > 0:
    nums.append(currentnum)
    currentnum = int(input('Enter Numbers List, to end  enter 0: '))
max = 1
count = 0
for num in nums:
     if num > max:
        max = num
        count = 1
     elif num == max:
        count += 1
print('The Largest number is:', max, 'and count is', count)

Now this can not be the efficient solution but try to understand the flow of a program. We create a list nums where we store all the input numbers and then we run the loop on it to find the max value and its count.

Best of luck :)

0

to make sure that operation stops at '0', regex is used. counting is done by iterating on a set from the input using a dict comprehension.

import re
inp = "3 5 2 5 5 5 0 other things"
inp = ''.join(re.findall(r'^[\d\s]+', inp)).split()
pairs = {num:inp.count(num) for num in set(inp)}
print(f"{max(pairs.items())}")
lroth
  • 367
  • 1
  • 2
  • 4
-1

Why not do it this way?

print(f"{max(lst)} appears {lst.count(max(lst))} times")
Elicon
  • 206
  • 1
  • 11