Python Forum

Full Version: 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 3
Hey guys- I'm on my last assignment for Python and I need some expert assistance please.

This is 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.

This is my code:

largest = None
smallest = None
while True:
    try:
        num = raw_input("Enter a number: ")
        if num == "done":
            break
        print (num)
        num = int(num)
        if largest is None or largest < num:
            largest = num
        elif smallest is None or smallest > num:
             smallest = num
    except ValueError:
        print("Please, enter only numbers.")

print ("Maximum", largest)
print ("Minimum", smallest)
This is the Desired output:
Invalid input
Maximum is 10
Minimum is 2


This is my output:
7 ← Mismatch
2
bob
Please, enter only numbers.
10
4
Maximum 10
Minimum 2


I need help please. Wall
What's wrong with the output you're getting?
(Oct-07-2017, 04:39 PM)Lux Wrote: [ -> ]What's wrong with the output you're getting?

Check out my screenshot below:

5.2py assignment screenshot

I'm stumped. I'm tried everything I know to no avail. Desperately need some help on this.

Thank you all!
Well, the line print(num) seems to be why it's printing all the numbers- not sure if it's supposed to do that.
That did fix part of the problem, but the output is a still mismatch. The desired output is the same; but something is still not right...

Screenshot:
Screenshot of assignment and code with error:

Thanks again for your time.
I found that website and tried it- you're doing everything right, but it wants you to say "Maximum is" instead of just "Maximum".
Lux, I cannot thank you enough for helping me get through this!

That was it! Thank you, gratefully!
you need to get the output exactly as mentioned. here's my code:


largest = None
smallest = None
while True:
try:
num = input("Enter a number: ")
if num == "done":
break

num = int(num)
if largest is None or largest < num:
largest = num
elif smallest is None or smallest > num:
smallest = num
except ValueError:
print("Invalid input")

print ("Maximum is", largest)
print ("Minimum is", smallest)
[quote="baba04201" pid="27251" dateline="1507391893"]Hey guys- I'm on my last assignment for Python and I need some expert assistance please. This is 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. This is my code:
largest = None smallest = None while True: try: num = raw_input("Enter a number: ") if num == "done": break print (num) num = int(num) if largest is None or largest < num: largest = num elif smallest is None or smallest > num: smallest = num except ValueError: print("Please, enter only numbers.") print ("Maximum", largest) print ("Minimum", smallest) 
This is the Desired output: Invalid input Maximum is 10 Minimum is 2 This is my output: 7 ← Mismatch 2 bob Please, enter only numbers. 10 4 Maximum 10 Minimum 2 I need help please. Wall[/q


Here is my solution to this problem:
largest = None
smallest = None
while True:
inp = raw_input("Enter a number: ")
if inp == "done" : break
try:
num = int(inp)
except:
print("Invalid input")
if smallest is None:
smallest = num
elif num < smallest:
smallest = num
elif num > largest:
largest = num

continue

print(("Maximum is"), largest)
print(("Minimum is"), smallest)
please, solve my problem, when i put the term in output block(7,2,bob,10,2)again it ask enter a num and not show the result
Pages: 1 2 3