-2
def algop(num):
    try:
        if num == 0:
            return "The number is neither positive nor negative"
    except:   
        sally = num + 1
        if num - sally == -1:
           return int(num), str("is positive")
        else:
           return int(num), str("Is negative")

print(algop(10))

The answer I get is "none". How can I get this to work?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    Just remove the `try`/`except`? – mkrieger1 Jan 22 '21 at 00:54
  • 1
    Also, why not use `< 0` and `> 0`? The `sally` variable doesn't seem to make any sense. You are effectively testing `-1 == -1`. – mkrieger1 Jan 22 '21 at 00:56
  • Stack Overflow is not intended to replace existing tutorials and documentation. Please repeat your materials on `try-except` so that you understand their function and when to use them. – Prune Jan 22 '21 at 01:00

2 Answers2

1

Don't use try and except, they are for error handling. Instead try:

def algop(num):
    if num == 0:
        return "The number is neither positive nor negative"
    sally = num + 1
    if num - sally == -1:
        return int(num), str("is positive")
    else:
        return int(num), str("Is negative")

print(algop(10))

Also you don't need to do that code for checking positive or negative, just do < and >, also you could just do f-strings:

def algop(num):
    if num == 0:
        return "The number is neither positive nor negative"
    if num > 0:
        return f'{num} is positive'
    else:
        return f'{num} is negative'

print(algop(10))

Both codes output:

10 is positive
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

You should only use the Try: except: if your script stops due to an error ;)

def algop(num):
  if num == 0:
    return "The number is neither positive nor negative"
  elif num > 0:
    return str(num) + "is positive"
  else:
    return str(num) + "Is negative"

print(algop(10))
Fredericka
  • 296
  • 1
  • 7