Python Forum
stuck in a block of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
stuck in a block of code
#1
Hello

I am trying to run this block of code and I am getting syntex error that says
" File "lopp2.py", line 17
if smallest is None :
^
SyntaxError: invalid syntax"

I am using atom text editor and python 3
largest = None
smallest = None
while True:
    try:
        num = input('Enter a number: ')
    if smallest is None :
        smallest = num
    elif num < smallest :
        smallest = num
    elif num > smallest
        largest = num
	elif num == 'done' :
        break
    except Invalidnum:
        print('Invalid Input')
print("Maximum", largest,smallest)
Reply
#2
The try block needs to be followed by an except block. You have your if/elif blocks in between, that's the syntax error. They either need to be indented one level (so the if is even with num = ..., or they need to be moved after the except block. You can have an else after the except that is tied to the try, that runs if no exception is raised, but you can't put if and elif in between

Also, I don't know what you are trying to do with Invalidnum. That's not a standard exception, so it wouldn't be raised by any of the code you have there. You would have to create it as a subclass of another exception and raise it manually. That looks like you are trying to catch an error after converting the string returned by input into a number. However, you are not doing that conversion, and an error there would raise a ValueError exception.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with this code and am stuck TheBoonester 2 2,311 Sep-19-2019, 07:56 PM
Last Post: j.crater
  I have homework that I've made no effort to show any code that I'm stuck on Ronaldinho 1 2,383 May-30-2019, 07:18 PM
Last Post: nilamo
  Trying to teach myself how to code and I'm stuck on a question I found online... abushaa4 1 2,209 Dec-16-2018, 01:52 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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