Python Forum
Python for everyone course assignment 5.2 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Python for everyone course assignment 5.2 (/thread-23341.html)



Python for everyone course assignment 5.2 - ofekx - Dec-23-2019

The assignment goal is to create a program that gets numbers as input from the user and i need to send back the largest number and the smallest number
num = input("Enter a number: ")
largest = num
smallest = num
while True:
    num = input("Enter a number(type ""done""to finish):" )
    try:
        int(num)
    except ValueError:
        if num != "done":
            print("invalid input")
            continue
    if num == "done": break
    if largest < num:
        largest = num
    elif smallest > num:
       smallest = num

print("Maximum is ",largest)
print("Minimum is ",smallest)



RE: Python for everyone course assignment 5.2 - ichabod801 - Dec-23-2019

Do you have a question?


RE: Python for everyone course assignment 5.2 - ofekx - Dec-23-2019

Yes for some reason i cannot understand my code has alot of problems when it runs


RE: Python for everyone course assignment 5.2 - nilamo - Dec-23-2019

Quote:num = input("Enter a number(type ""done""to finish):" )

That would print out to look like: Enter a number(type doneto finish):
If you want the quotes to be printed out, you need to either:
1) escape them in the string like so: num = input("Enter a number(type \"done\"to finish):" )
2) use single quotes for the overall string, like so: num = input('Enter a number(type "done"to finish):' )
or 3) use triple quotes for the overall string: num = input("""Enter a number(type "done"to finish):""" )

Quote:if largest < num:
At this point, num is still a string. And comparing a string to an int doesn't make sense. You're already using the int() function to check if they gave you a number, so why not use it's returned value (the int)?
Something like
response = input("Give number: ")
if "done" == response:
    # do something
    pass
else:
    try:
        num = int(response)
        if largest < num:
            largest = num
    # etc