Oct-03-2017, 07:44 PM
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:
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):
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.
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:
1 2 3 4 5 6 7 8 |
while True : num = input ( "Enter num: " ) if num = = "done" : break try : pnum = int (num) except : print ( "invalid input" ) |
1 2 3 4 5 6 7 8 9 10 |
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) |
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.