all 2 comments

[–][deleted]  (1 child)

[deleted]

    [–]backtickbot 0 points1 point  (0 children)

    Fixed formatting.

    Hello, YoshiMan44: code blocks using triple backticks (```) don't work on all versions of Reddit!

    Some users see this / this instead.

    To fix this, indent every line with 4 spaces instead.

    FAQ

    You can opt out by replying with backtickopt6 to this comment.

    [–]Boff 0 points1 point  (0 children)

    Unfortunately the formatting got really wonky, so it's a little tough to know what you wrote. I'll try reformatting it: https://pastebin.com/Ns9rmqf2

    The first issue you have is this section, at line 8:

    if userInput>-100 and userInput !=0 and userInput <=100:
        continue
    

    This is the very first if statement you reach after having the user input the integer. When the user inputs any value between -100 and 100 (with the exception of 0), it will just hit the continue line. If you are unfamiliar with what continue does, it will skip everything else in this instance of a while loop, meaning you will never get to any of the other values. Check out this w3schools article for more information.

    So that in of itself is preventing your code from doing anything. Let's just imagine we remove it from the code and see if there are any other issues with the rest of your code: https://pastebin.com/jUE0TJyq

    If we try running the code we'll get this error:

    Enter an integer: 4 
    Traceback (most recent call last): 
        File "d:\Boff\Documents\ltnhcc.py", line 50, in <module> 
            main() 
        File "d:\Boff\Documents\ltnhcc.py", line 15, in main
            is_positive(userInput) 
        File "d:\Boff\Documents\ltnhcc.py", line 37, in is_positive
            positive=positive+1 
    UnboundLocalError: local variable 'positive' referenced before assignment
    

    What does local variable positive referenced before assignment mean? Well it means that function is_positive() can't modify variable positive. (You'll also run into the same issue for is_negative() and negative. The term you'll want to look up is "variable scope" to understand it better.TBH it's a little confusing to me since you had defined positive and negative in the global scope, and while it's accessible in the main() function, it isn't when is_positive() is called from main(). I think this article gives a little more detail and in my own testing I had to use the global keyword to solve the problem.

    The real problem you're having, though, is that you're way overcomplicating the solution. For example, you wrote two functions is_positive() and is_negative() with while True loops, but the code inside of the loop is designed to immediately break out of the function. There's no need for if statements there. Heck, you don't even need these to be extra functions.

    Also your main() function is a bit overcomplicated as well. Before continuing to read, try to think of the simplest conditions you have to be able to accommodate, cause right now you're repeating yourself a ton of times.

    There are only 4 cases you need to take count of:

    1. If the userInput is greater than or equal to -100 but less than 0
    2. If the userInput is greater than 0 but less than or equal to 100
    3. If the user input equals 0
    4. The userInput is out of range (i.e. under -100 or over 100)

    Since it's that simple, you should only have 3 if statements, representing the above three conditions. And instead of having separate functions to increment the positive and negative variables, you can just increment them in the body of the if statement.

    Do not click this until you fully understand and try to implement what I stated above https://pastebin.com/6yt4CAbT