0

I am trying make a script that will accept 3 inputs first is username as input second and third are bar codes i need to compare, but after the first input of the user I want it to stop asking me for the user and just save and use it and cant seem to do it. I am assigning the user_global to be None at start so can I use an if to run the get_user function but when the script runs it second loop time it gives None as value to the user again and i cant seem to remember how to remove the user from the loop and save it after the first iteration because i am real dumb, as I stated before. Here is the code:

while True:

def get_user():
    user=input("Enter User :")
    user_global = user
    print(user)
    print(user_global)

user_global = None


if user_global == None:
    get_user()


a = datetime.datetime.now()
print(a)

def gun_read():
    barcode1=input("Please the first barcode the barcode!?")
    print(barcode1)
    barcode2=input("Plese read the secdon barcode?!")
    print(barcode2)
    if barcode1 == barcode2:
        print("GREEN LIGHT!!")
    else:
        print("you fcked up broooo!!")


# if os.cwd() ima csv file add to csv else create csv

gun_read()

Help, please ? Also ignore prints and datetime.

Craicerjack
  • 6,203
  • 2
  • 31
  • 39
miloshIra
  • 53
  • 5
  • You have some indentation error in your code. Which statements are supposed to run in the `while` loop? Also, what is the condition for exiting the loop? – mapf May 06 '20 at 08:43

3 Answers3

0

By default, Python doesn't allow you to assign global variables. In get_user() what it's doing is making a new variable called global_user. To change this behaviour, have global user_global on a line before you assign the variable.

Misha Melnyk
  • 409
  • 2
  • 5
0

Unless I'm missing something,

user = None

def get_user():
    return input("Enter User :")


def gun_read():
    barcode1 = input("Please the first barcode the barcode!?")
    print(barcode1)
    barcode2 = input("Plese read the secdon barcode?!")
    print(barcode2)
    if barcode1 == barcode2:
        print("GREEN LIGHT!!")
        return True
    else:
        print("you fcked up broooo!!")
        return False


def main():
    global user  # so we can write to the global user variable
    user = get_user()  # grab the username and write it
    while True:  # repeat gun reads until the end of time
        gun_read()


if __name__ == "__main__":
    main()
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Cheers mate, this works for me I am so rusty i didn't even think of def main(), thanks for that too you are a trooper. – miloshIra May 06 '20 at 10:36
0

first of all, keep your def functions always on top for readability, second of all why is the whole thing in while True:? And lastly in get_user() you can call gun_read()

def get_user():
    user=input("Enter User :")
    user_global = user
    print(user)
    print(user_global)
    gun_read()

def gun_read():
    barcode1=input("Please the first barcode the barcode!?")
    print(barcode1)
    barcode2=input("Plese read the secdon barcode?!")
    print(barcode2)
    if barcode1 == barcode2:
        print("GREEN LIGHT!!")
    else:
        print("you fcked up broooo!!")

user_global = None
while True:
    if user_global == None:
        get_user()
    else:
        gun_read()
peter123
  • 181
  • 10
  • 1
    This code still asks to enter user every iteration, but no worries the mate on top fixed it for me, and it is in while True because that's the easiest solution to run it forever, i guess. – miloshIra May 06 '20 at 10:40