[Python] PyCharm warns local variable might be referenced

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    Why does PyCharm highlight the boolean variable nearby the return with Local variable "boolean" might be referenced before assignment?

    This code checks whether a number is prime or not:

    import random
    import math
    import time
    def prime_t(x):
    print x
    if x < 2:
    return False
    if x == 2:
    return True
    if x == 3:
    return True
    for i in range(2, int(math.sqrt(x))+1):
    if x % i == 0:
    boolean = False
    break
    else:
    boolean = True
    return boolean
    random.seed()
    how_much = input()
    start = time.time()
    for i in range(0, how_much):
    print(prime_t(random.randint(0, 1000)))
    print time.time()-start


    I've read that might be some problem with global variables, but there's no ones which might be used in prime_t(). I had similar thing - exception while executing the code, but I think it has been eliminated with if x == 2 and if x == 3.

    What else might be the problem?

    Continue reading...

Compartilhe esta Página