Python Forum
[split] Python for Everybody 5.2 assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Python for Everybody 5.2 assignment
#1
largest = None
smallest = None
while True:
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        fnum = float(num)
    except:
        print("Invalid input")
        continue
    if largest is None :
    	largest = float(num)
    elif num > largest :
        largest = float(num)
    if smallest is None :
        smallest = float(num)
    elif num < smallest :
    	smallest = float(num)
    print(num)
    print("Min:",smallest)
    print("Max:",largest)
print("Maximum is",int(largest))
print("Minimum is",int(smallest))
Can someone help point to where I have gone wrong with the logic please? Thanks!
Reply
#2
Can you be more specific? What inputs are you using? How exactly is the behavior different from what you want?
Reply
#3
(Jun-14-2019, 04:38 PM)jonchanzw Wrote: elif num > largest :

num is a string. largest is a float. You have an fnum variable available, why not use that for comparisons?
Reply
#4
Here the code will help you...……
largest = None
smallest = None
while True:
try:
num = input("Enter the 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
#5
As far as my googling went I understood that there are basically no limitations regarding solution.

So one can take advantage of Python 3.8 new cool feature and of course use built-in min and max to deliver result:

ints = []

while (answer := input('Enter integer: ').lower()) != 'done':
    try:
        ints.append(int(answer))
    except ValueError:
        print(f"{answer!r} can't be interpreted as integer")

print(f'Maximum entered value was {max(ints)} and minimum value was {min(ints)}')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] University Assignment Help needed Nomathemba 4 3,534 Apr-20-2024, 12:24 PM
Last Post: jas31
  Python for Everybody 5.2 assignment baba04201 20 173,321 Jul-25-2023, 04:15 PM
Last Post: MicaelSchutz
  [split] assignment help Shobana 2 1,309 Nov-11-2022, 12:10 PM
Last Post: Larz60+
  Python for Everybody 3.1 assignment ramadan2099 18 45,405 Jan-23-2021, 06:27 AM
Last Post: KonohaHokage
  Coursera python for everybody 5.2 assignment SteppentigerV2 11 12,856 Oct-22-2020, 11:57 AM
Last Post: Larz60+
  [split] Python for Everybody 5.2 assignment ramadan2099 3 12,056 Jul-15-2020, 04:54 PM
Last Post: Bipasha
  Python Assignment 3 - Applied Data Science - 2.3 eyavuz21 8 4,959 Jun-06-2020, 08:59 AM
Last Post: eyavuz21
  Python Password Saver Assignment sshellzr21 2 6,162 May-02-2020, 01:34 AM
Last Post: sshellzr21
  Python for Everybody 3.3 assignment ramadan2099 7 31,745 Apr-08-2020, 06:49 AM
Last Post: DeaD_EyE
  Python for everyone course assignment 5.2 ofekx 3 8,541 Dec-23-2019, 08:41 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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