Python Forum

Full Version: Coursera python for everybody 5.2 assignment
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm stuck with this. This is the requirement 5.2 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.

My code:

largest = None
smallest = None
while True:
    num = float(input("Enter a number: "))
    for i in num:
     if i > largest:
     largest = i
    print(largest)
    break
    for i in num:
    if num <smallest
    smallest=num
    print(num)
    except
    print("Invalid input)
    if num == "done" : break
    print(num)

print("Maximum is", largest, "Minimum is", smallest)
I guess the python interpreter is not very happy with your code. The important part is the error message sent by python. By reading it carefuly you can find where there is an error in the code. You can post the error message here between error tags.
The error is bad input on line 7
Well my python interpreter is more talkative. How do you run the code?
Error:
File "paillasse/tmp/badinput.py", line 7 largest = i ^ IndentationError: expected an indented block
It tells me that line 7 has a bad indentation. You need to add more space at the beginning of the line. It must not be aligned with the 'if' of the previous line.
I was using that py4e.com playgound. Does visual studio do it?
In python programming, it is very important to have the whole error message at execution. In principle, all IDEs print the error traceback. If you execute python code in a terminal, you also have the error traceback. This error message is a major feature of Python because it makes program very easy to debug.
(Sep-02-2020, 11:35 AM)SteppentigerV2 Wrote: [ -> ]I was using that py4e.com playgound
Dos in not print the whole Traceback?
I guess is dos,if it do not then is useless.
(Sep-02-2020, 11:35 AM)SteppentigerV2 Wrote: [ -> ]Does visual studio do it?
All editor should give show that Traceback output,and very few in the Python world use Visual Studio.
A lot use VS Code which is not the same as Visual Studio.
Is VS Code if a Linter if turned on give a Warning before running the code.
Here a example how this look,the linter i use here is Pylint.
[Image: uvSEZ5.png]
I wrote a code which gives valid output,
but still I get message "you should actually compute the maximum and minimum".
What do they want from me? :)

l = 0
s = 1000000
while True:
num = input("Enter a number: ")
if num == "done": break
try: n = int(num)
except: print('Invalid input')
if n > l: l = n
if n < s: s = n
continue

print("Maximum is", l)
print("Minimum is", s)
Please use the Python tags when posting code.

Without knowing the assignment, tough to know what they are asking. This works for integers, not floats, and only in a particular range. A set of negative numbers will give the wrong answer.

What was the assignment?
5.2 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.
Pages: 1 2