Python Forum
Python for Everybody 5.2 assignment - 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 Everybody 5.2 assignment (/thread-5498.html)

Pages: 1 2 3


Python for Everybody 5.2 assignment - baba04201 - Oct-07-2017

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


RE: Python for Everybody 5.2 assignment - Lux - Oct-07-2017

What's wrong with the output you're getting?


RE: Python for Everybody 5.2 assignment - baba04201 - Oct-07-2017

(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!


RE: Python for Everybody 5.2 assignment - Lux - Oct-07-2017

Well, the line print(num) seems to be why it's printing all the numbers- not sure if it's supposed to do that.


RE: Python for Everybody 5.2 assignment - baba04201 - Oct-07-2017

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.


RE: Python for Everybody 5.2 assignment - Lux - Oct-07-2017

I found that website and tried it- you're doing everything right, but it wants you to say "Maximum is" instead of just "Maximum".


RE: Python for Everybody 5.2 assignment - baba04201 - Oct-07-2017

Lux, I cannot thank you enough for helping me get through this!

That was it! Thank you, gratefully!


RE: Python for Everybody 5.2 assignment - zaid03 - May-31-2018

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)


RE: Python for Everybody 5.2 assignment - Shubham - Dec-12-2019

[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)


RE: Python for Everybody 5.2 assignment - vis1999 - Apr-07-2020

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