Python Forum
My code doesn't work, can someone help me?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My code doesn't work, can someone help me?
#2
You are using num for the input, then expect that num will end up being the list that you can cycle over, and you also never append the integer to the list.
Minor changes, try:
largest = None
smallest = None
num = []
while True:
    num_input = input("Enter a number: ")
    if num_input == "done" : 
        break
    try:
        num1 = int(num_input)
        num.append(num1)
    except ValueError:
        print('Invalid input')
        continue
for number1 in num:
    if largest is None:
        largest = number1
    elif largest > number1:
        largest = number1
print("Maximum is", largest)
for number2 in num:
    if smallest is None:
        smallest = number2
    elif smallest < number2:
        smallest = number2
print("Minimum is", smallest)
Output:
Enter a number: 2 Enter a number: 8 Enter a number: 9 Enter a number: python Invalid input Enter a number: 4 Enter a number: 10 Enter a number: done Maximum is 2 Minimum is 10
Reply


Messages In This Thread
RE: My code doesn't work, can someone help me? - by jefsummers - Dec-21-2020, 01:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I can't for the life of me get this basic If statement code to work CandleType1a 8 555 May-21-2024, 03:58 PM
Last Post: CandleType1a
  Extending list doesn't work as expected mmhmjanssen 2 385 May-09-2024, 05:39 PM
Last Post: Pedroski55
  hi need help to make this code work correctly atulkul1985 5 1,046 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 855 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 1,105 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 871 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Beginner: Code not work when longer list raiviscoding 2 985 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 2,135 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,671 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 1,047 Dec-29-2022, 08:59 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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