Python Forum

Full Version: Python for everyone course assignment 5.2
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
Do you have a question?
Yes for some reason i cannot understand my code has alot of problems when it runs
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