Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loops, min and max
#1
Hello,

I'm new here in this forum. I'm new in programming too, and, as you can imagine, I'm to find a solution to a problem (or, better, some hint, so I can solve it by myself). I'm attending an online course for beginners, trying to learn something about Python.
The problem is that I have no idea what to do with one of the last exercises. I'm not here to find the solution, but only to ask for some hint.
The text is:

"Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below."

I wrote a code that's ok to let the user enter numbers, to stop the loop when the user enters "done" and to recognize invalid inputs (like "bob" in the text), but I have no idea what to do to make the program single out the inputs and tell which of them is the largest and which the smallest.

The code should be something like this:

while True:
 num = input("Enter num: ")
 if num == "done":
  break
 try:
  pnum = int(num)
 except:
  print("invalid input")
That's good if we want to enter only numbers and the word "done", but it's not enough to solve the problem. I tried to add this (but I knew the compiler would have not accepted it):

the_largest_so_far = -1
for num in [7, 2, 10, 4]:
 if num > the_largest_so_far:
  the_largest_so_far = num
print(the_largest_so_far)
the_smaller_so_far = 20
for num in [7, 2, 10, 4]:
 if num < the_smaller_so_far:
  the_smaller_so_far = num
print(the_smaller_so_far)
Actually it prints out exactly 10 and 2, but that's not what the compiler wants to read.
Every time the program tells the user to enter another number, the last number entered becomes lost. So it can't memorize it and use it again to tell if it is the greater, the smaller or something between.
I think it's enough to understand my situation. Maybe for most of you it's just a simple problem, but I can't solve it. I hope someone will help me.
Reply
#2
Start with keeping track of the numbers you get.  A list would be great for this.

Are you allowed to use built-in functions?  Because min/max is pretty easy if you can... https://docs.python.org/3/library/functions.html#min
Reply
#3
I'm confused. If you put together the two parts you have, that should solve the problem. What do you mean by 'that's not what the compiler wants to read'?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Oct-03-2017, 08:18 PM)ichabod801 Wrote: I'm confused. If you put together the two parts you have, that should solve the problem. What do you mean by 'that's not what the compiler wants to read'?

I mean that to solve the problem I have to write a code that takes every numbers entered by the user and than tells him which of them is the largest and which of them is the smallest. The second part of my code takes the elements of a list created independently from the inputs.

(Oct-03-2017, 07:59 PM)nilamo Wrote: Start with keeping track of the numbers you get.  A list would be great for this.

Are you allowed to use built-in functions?  Because min/max is pretty easy if you can... https://docs.python.org/3/library/functions.html#min

Yes, I'm allowed to use built-in functions. I'll try that way, thank you.
Reply
#5
You are basically there but are over complicating things.

You have a block of code that will keep taking input until done is encountered, and which will ignore bad inputs.

Assuming the inputs needs to be within a certain numeric range, say between 0 and the maxim integer value of the system you are running on (which is given by sys.maxsize if you import sys at the top of your programme), then you only need to set your smallest and largest answers to the largest and smallest numbers in the possible range, and then change them if you encounter anything smaller or larger respectively.

No lists required.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#6
Thank you all for the answers. I'm still trying to follow nilamo's hints.
That's what I wrote:

list = []
while True:
    num = input("Enter num: ")
    list.append(num)
    if num == "done":
        break
    try:
        pnum = int(num)
    except:
        print("Invalid input")
        continue
del list[2]
del list[4]
largest = None
for num in list:
    if largest is None:
        largest = num
    elif num > largest:
        largest = num
print("Maximum is",largest)
smallest = None
for num in list:
    if smallest is None:
        smallest = num
    elif num < smallest:
        smallest = num
print("Minimum is",smallest)
I thought that it would have been a good idea to create an empty list and to add elements for every input given by the user. The exercise impose to enter "7, 2, bob, 10, 4". 7, 2, 10 and 4 to use them to tell which one is the largest and which one is the smaller. "Bob" is just an example of invalid input. The problem was that "list.append(num)" put in the list "bob" and "done", and I don't know how to tell the program to take only numbers, so I added "del list [2]/[4]" to delete the third and the fifth element. I tried to use this code to solve the problem, but the result is that the largest number is 7 and the smallest is 10 (and I don't know why). So i tried something else.

while True:
    num = input("Enter num: ")
    if num == "done":
        break
    try:
        pnum = int(num)
    except:
        print("Invalid input")
        continue
largest = None
for num in [7, 2, 10, 4]:
    if largest is None:
        largest = num
    elif num > largest:
        largest = num
print("Maximum is",largest)
smallest = None
for num in [7, 2, 10, 4]:
    if smallest is None:
        smallest = num
    elif num < smallest:
        smallest = num
print("Minimum is",smallest)
This is almost the same as the second part of the code I wrote at the beginning of this thread. The result is the right one: "Maximum is 10" and "Minimum is 2". But there's another problem. When I write the code on the site and click on "Check code" it tells me that I should compute the maximum and the minimum. I really don't understand what it wants me to do.

Two question:

1) Why in the first case I get minimum = 10 and maximum = 7?
2) What do you think "You should actually compute the maximum and the minimum" means?
Reply
#7
To be sure only numbers get in the list, wait until you're sure you have a number to put it into the list. That would be right after pnum = int(num). At that point you can also do both your smallest and largest checks, so when the input is done you already have your answers.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020