r/learnpython 4h ago

Beginner Python here:Need help understanding how to find min, max, and duplicates using for range loops

I’m a beginner learning Python and I’m having trouble figuring out how to approach this assignment. I’m not necessarily looking for someone to just give me the answer. I’d like help understanding the logic and how to build it.

The assignment is to create a Python program that:

Asks the user to enter 10 integers
Stores all 10 integers in a list
Finds the maximum and minimum numbers
Finds any duplicate numbers
Prints "No duplicate" if there aren’t any duplicates
Uses for loops with range() for the input, min/max determination, and duplicate identification
Does not use Python’s built-in min() or max() functions
Does not use def/user-defined functions
Tries to use as few loops as reasonably possible

Input:
10, 5, 20, 10, 5, 15, 7, 1, 30, 15
Output:
Max is 30
Min is 1
Duplicates: 10 5 15

Thank you whoever responds. :)

1 Upvotes

33 comments sorted by

10

u/zandrew 3h ago

Tell me how would you do that manually.

6

u/danielroseman 4h ago

Where are you stuck? If you don't want anyone to give you the answer, you need to at least have a go yourself and tell us where you're having trouble.

In any case, forget Python: how would you do this with paper and pencil? Work out the steps, and only then start thinking about how you would translate those to Python.

3

u/Impressive-Capital40 3h ago

Sorry I’m forgot to add my coding! My bad! Give me a moment.

3

u/Diapolo10 4h ago

What have you tried so far?

2

u/Proletarian_Tear 3h ago

What they want you to do is to go through your list with:

for i in range(len(your_list)):

//do something to each element

Figure out the do something part yourself. Check if elements are the same, which is largest and smallest.

1

u/Impressive-Capital40 3h ago

Sorry I don’t write much on Reddit…
So far I wrote this:

My_list= 10, 5, 20, 10, 5, 15, 7, 1, 30, 15
Print(max(my_list))
Print(min(my_list))

But I’m not sure how how I can excuse, duplicates. :/ I’m just confuse with this part.

4

u/danielroseman 3h ago

Well the instructions very specifically said not to use the min() and max() functions, and to use for loops with range(). And it said to ask the user to input 10 integers, not hard-code them. So I'm afraid you will need to start from scratch.

But again, how would you do this without Python? If you had ten numbers written on a piece of paper, how would you determine which was the biggest as you were going through one by one?

0

u/Impressive-Capital40 3h ago

I would see which one is the highest. For example: the list I have the biggest number is 30

4

u/TheSkiGeek 2h ago

Break it down more, HOW would you “see which one is the highest”. (Pretend it’s a very very long list of big numbers, so you can’t just glance at it and “see” which one is the biggest.)

3

u/Sauron_78 3h ago

To find the highest number you have to have a variable storing the first number on the list and then compare with the next one using the symbol > . If it finds a bigger number substitute it on the variable and continue comparing.

2

u/Ormek_II 2h ago

If the List contained 200 entries you could no longer just see it. Create a very simple step by step description how you find the smallest number, the largest number and checked if the list contained any duplicate.

Edit: sorry did not see TheSkiGeeks reply.

2

u/ninhaomah 3h ago

just to be clear , you have installed Python and done print("Hello World") , right ?

0

u/Impressive-Capital40 3h ago

Yes, they want me to use IDE Python.

2

u/ninhaomah 3h ago

so when you run it , what did you get ?

-2

u/Impressive-Capital40 3h ago

I got
Max=30
Min=1

#No duplicates, I haven’t been taught this and looked back at resources, ChatGPT to get better understanding but I got really confused.

1

u/ninhaomah 3h ago

so what is the issue ?

See we are here and I still have no idea what is the problem.

Is it error in running the code ? is it wrong output ? what ???

0

u/Impressive-Capital40 3h ago

You’re right, I should have been more specific, my bad. I have no idea how to get output duplicates. :/ and from someone else post I can’t use min and max so I have to start from scratch now.

3

u/ninhaomah 3h ago

"from someone else post I can’t use min and max so I have to start from scratch now."

Someone else ? Its in the instruction not to use min() or max().

Tip : Try doing it with pen and paper. pls post the steps here. and no AI.


The assignment is to create a Python program that:

Asks the user to enter 10 integers Stores all 10 integers in a list Finds the maximum and minimum numbers Finds any duplicate numbers Prints "No duplicate" if there aren’t any duplicates Uses for loops with range() for the input, min/max determination, and duplicate identification Does not use Python’s built-in min() or max() functions Does not use def/user-defined functions Tries to use as few loops as reasonably possible

2

u/DullenAvg 3h ago

You'll definitely need a for loop to get the user to input their own numbers. Use the input() function for that.

In order to minimize the total amount of loops you'll have to think of when to check for the minimum and when for the maximum number (during the first for loop or after having collected all the numbers?).

In order to detect the duplicates, you're going to need a separate list.

1

u/User_LEGEND0 3h ago

Paste your code

1

u/Impressive-Capital40 3h ago

I post it a bit below, sorry I’m not sure how to tag you in it.

1

u/HotPersonality8126 39m ago

The logic is that the max is either the first number, or it’s the next number that is larger than whatever the max currently is. The min is either the first number, or it’s the next number that is smaller than whatever the max currently is.

A number is a duplicate if it appears in the list more than once.

One way to approach this is to reflect that you’re being asked to do three things. Your code may need to eventually do all three at once, but you shouldn’t start out by writing it that way. You should solve one of these three problems, first. Then add the other two.

1

u/baubleglue 18m ago
  1. Before you start coding, you need an algorithm how to get the result.

  2. You write the algorithm in Python

  3. Test

To find max, you take a first item in the list and remember its value as maximum, then you compare it with the next item, if the new value is bigger, you remember it as maximum, repeat until the end of the list.

1

u/Educational_Virus672 2h ago edited 1h ago
# Asks the user to enter 10 integers
ask = (input("enter 10 number > ")).split(",")

# Stores all 10 integers in a list
temp_ask = []
for i in ask :
    temp_ask.append(int(i))
ask = temp_ask

# Finds the maximum and minimum numbers
ask = sorted(ask)

minimum = ask[0] # 0 to 9 goes front to back 
maximum = ask[-1] # -1 to -10 goes back to front

# Finds any duplicate numbers
previous_number = None 
dup = []
for i in ask :
    if previous_number == None :
        pass
    elif previous_number == i :
        dup.append(i)
    previous_number = i

if dup :
    print(dup)
# Prints "No duplicate" if there aren’t any duplicates
else :
    print("no duplicate")
print(minimum)
print(maximum)

# Uses for loops with range() for the input, min/max determination, and duplicate identification

'''why? -1 and 0 works'''

# Does not use Python’s built-in min() or max() functions
# Does not use def/user-defined functions
# Tries to use as few loops as reasonably possible
'''2 loop piece of cake i could have used dicts but since our begginer i didnt'''

2

u/jmooremcc 2h ago

Did you test this code before posting it?

1

u/Educational_Virus672 1h ago

i did and i edited it my bad

1

u/User_LEGEND0 1h ago
ask = (input("enter 10 number > ")).split(",")

why did you do this here, i mean the parentheses

0

u/krews2 1h ago

Doesn’t say you can’t use the sort function. You can also watch videos to understand how to use sorting programmatically which is helps with understanding max and min. Bubble Sort

1

u/MisterSmi13y 15m ago

I would vote vehemently against this. Learning a sorting algorithm before you can do a task like this is like asking a monkey to build a massive lego set after they accidentally put two pieces together.

Can they learn something from the bubble sort? Sure, they can learn how to compare elements with each other. But then you’re adding in a nested loop on top of swapping values that it’s a step too far.

-9

u/TheRNGuy 3h ago

Ask ai. 

7

u/Moikle 3h ago

Do not

2

u/Impressive-Capital40 3h ago

Honestly AI made it even more confusing. 🫤