Home > Mobile >  module 'Lowesthighest' has no attribute 'l' while it has a attribute defined as
module 'Lowesthighest' has no attribute 'l' while it has a attribute defined as

Time:01-05

I am making a simple number guesser game and for some reason i get the error:

Traceback (most recent call last):
  File "f:\Coding\Microsoft VS Code\CODES\Projects\Number Guesser\Main.py", line 3, in <module>
    Guess.guesser()
  File "f:\Coding\Microsoft VS Code\CODES\Projects\Number Guesser\Guess.py", line 5, in guesser
    numguess = int(input("Guess a number between"   Lowesthighest.l   "and"   Lowesthighest.h))
AttributeError: module 'Lowesthighest' has no attribute 'l'

Here i will put all of my code (i have it in diffrent files):

Guess.py

import Lowesthighest


def guesser():
    numguess = int(input("Guess a number between"   Lowesthighest.l   "and"   Lowesthighest.h))

    if numguess > Lowesthighest.h:
        print('Your guess, '   '"{}"'.format(numguess)   "is more than "   Lowesthighest.h)
    elif numguess < Lowesthighest.l:
        print('Your guess, '   '"{}"'.format(numguess)   "is less than "   Lowesthighest.l)   
    else: 
        print("Congratulations, Your guess, "   '"{}"'.format(numguess)   "was correct!")

Lowesthighest.py


def lh():
    l = int(input("Lowest Number: "))
    h = int(input("Highest Number: "))

Main.py

import Guess

Guess.guesser()

I think it is due to the reason l and h are inside a function and i try to call it only through the file, might be wrong but i don't know.

Any help appreciated!

CodePudding user response:

  • make the variables l and h global.
  • call the function
global l
global h
def lh():
    l = int(input("Lowest Number: "))
    h = int(input("Highest Number: "))
lh()

or simply write this in your file, its much more easier:

l = int(input("Lowest Number: "))
h = int(input("Highest Number: "))

CodePudding user response:

Define the variables outside the function, and call them globally inside the function.

l, h = None, None
def lh():
    global l, h
    l = int(input("Lowest Number: "))
    h = int(input("Highest Number: "))

Keep in mind that this would only work if you called the function before calling the variables. Otherwise, it will return an error because they would be None

  •  Tags:  
  • Related