Python Forum
Python for everyone course assignment 5.2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python for everyone course assignment 5.2
#1
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)
Reply
#2
Do you have a question?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Yes for some reason i cannot understand my code has alot of problems when it runs
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python for Everybody 5.2 assignment baba04201 20 173,237 Jul-25-2023, 04:15 PM
Last Post: MicaelSchutz
  Python for Everybody 3.1 assignment ramadan2099 18 45,377 Jan-23-2021, 06:27 AM
Last Post: KonohaHokage
  Coursera python for everybody 5.2 assignment SteppentigerV2 11 12,812 Oct-22-2020, 11:57 AM
Last Post: Larz60+
  [split] Python for Everybody 5.2 assignment ramadan2099 3 12,038 Jul-15-2020, 04:54 PM
Last Post: Bipasha
  Python Assignment 3 - Applied Data Science - 2.3 eyavuz21 8 4,943 Jun-06-2020, 08:59 AM
Last Post: eyavuz21
  Python Password Saver Assignment sshellzr21 2 6,146 May-02-2020, 01:34 AM
Last Post: sshellzr21
  Python for Everybody 3.3 assignment ramadan2099 7 31,729 Apr-08-2020, 06:49 AM
Last Post: DeaD_EyE
  [split] Python for Everybody 5.2 assignment jonchanzw 4 8,455 Oct-22-2019, 08:08 AM
Last Post: perfringo
  Python Assignment 5.2 amos76823 3 15,963 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