0

so basically I'm trying to code a program that does something after you provide a correct key.

So, First I call Keycheck(Key) after getting Key from input then it irritates through a file with keys (each key is in a new line ("\n")).

def Keycheck(Key):

    KeyFile = open("keys.key","r", encoding='utf-8')

    for line in KeyFile:
        line1 = line.strip()
        fields = line1.split("\n")
        linekey = fields[0]
  
   
        if Key in linekey:
            
            Keypass = "true"
            
    
        elif Key != linekey:

            continue

Well that works fine but the problem I'm having is to pass the Keypass variable to the main program, I tried reading about Kwargs but I couldn't understand it.

So my question is how do I pass the Keyword variable out of the "def Keycheck(Key)" and into the "main program", if that's not possible is there any other way to implement a key check from keys already stored in a file ?

EDIT: By main program I mean

def func():
def func2():
Keycheck(Key):

main program:
Key = input("")
Keycheck(Key)
if Keypass == "true": 
   func()
   func2()
else:
break

Just a hint on how I want the program to work:

  1. it gets the key from input
  2. it calls Keycheck(Key) (or checks that the key is valid)
  3. it continues the program if the Key is in the file from Keycheck(key) If it does not contain the Key in the Keyfile then just print("wrong key try again") or something like that and retry everything
jejejeje1
  • 3
  • 2
  • 1
    "the problem I'm having is to pass the Keypass variable to the main program" What main program? Please show all relevant code that goes along with your question, including the code that goes with the 3 steps that you describe in English. – Code-Apprentice Aug 01 '20 at 01:43
  • My guess is you just need to learn about what we call "returning" a value from a function. Also, you should learn about boolean values. Use the boolean value `True`, not the string `"true"`. – Code-Apprentice Aug 01 '20 at 01:43
  • https://stackoverflow.com/questions/37088457/accessing-returned-values-from-a-function-by-another-function Perhaps this question can serve as an example of what you need to do. – Code-Apprentice Aug 01 '20 at 01:45

1 Answers1

0

As @Code-Apprentice mentioned, just return True if you find the key. Otherwise, return False loop is done.

def Keycheck(Key):

    KeyFile = open("keys.key","r", encoding='utf-8')

    for line in KeyFile:
        line1 = line.strip()
        fields = line1.split("\n")
        linekey = fields[0]
  
        if Key in linekey:            
            return True    # found key, return True
        
    return False    # loop is done, key not found  


# main program
if Keycheck('Key123'):
    print('Found Key')
else:
    print('Key Not Found')
Mike67
  • 11,175
  • 2
  • 7
  • 15
  • Hi thank you for answering I'm getting invalid syntax on "Return True" is there something I'm missing ? do I maybe need to import something ? Edit: Ah it should be return not Return hehe, – jejejeje1 Aug 01 '20 at 01:58
  • Sorry - Typo on my part :( – Mike67 Aug 01 '20 at 02:09
  • haha thank you anyways, btw it didn't loop through the whole file and only checked the first line so I added this ` if Key in linekey: return True # found key, return True elif Key not in linekey: continue else: return False # loop is done, key not found ` and now everything works fine !! – jejejeje1 Aug 01 '20 at 02:12