Python Forum
Python for Everybody 5.2 assignment
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python for Everybody 5.2 assignment
#1
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
Reply
#2
What's wrong with the output you're getting?
Reply
#3
(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!
Reply
#4
Well, the line print(num) seems to be why it's printing all the numbers- not sure if it's supposed to do that.
Reply
#5
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.
Reply
#6
I found that website and tried it- you're doing everything right, but it wants you to say "Maximum is" instead of just "Maximum".
Reply
#7
Lux, I cannot thank you enough for helping me get through this!

That was it! Thank you, gratefully!
Reply
#8
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)
Reply
#9
[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)
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python for Everybody 3.1 assignment ramadan2099 18 45,168 Jan-23-2021, 06:27 AM
Last Post: KonohaHokage
  Coursera python for everybody 5.2 assignment SteppentigerV2 11 12,704 Oct-22-2020, 11:57 AM
Last Post: Larz60+
  [split] Python for Everybody 5.2 assignment ramadan2099 3 11,979 Jul-15-2020, 04:54 PM
Last Post: Bipasha
  Python Assignment 3 - Applied Data Science - 2.3 eyavuz21 8 4,883 Jun-06-2020, 08:59 AM
Last Post: eyavuz21
  Python Password Saver Assignment sshellzr21 2 6,103 May-02-2020, 01:34 AM
Last Post: sshellzr21
  Python for Everybody 3.3 assignment ramadan2099 7 31,639 Apr-08-2020, 06:49 AM
Last Post: DeaD_EyE
  Python for everyone course assignment 5.2 ofekx 3 8,482 Dec-23-2019, 08:41 PM
Last Post: nilamo
  [split] Python for Everybody 5.2 assignment jonchanzw 4 8,402 Oct-22-2019, 08:08 AM
Last Post: perfringo
  Python Assignment 5.2 amos76823 3 15,914 Jan-17-2019, 07:34 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020