0

Sorry if I did someting wrong, is my first post.

I want to know how can I put this program in a def function.

thanks in advance.


    file = open("Moradores.txt", "a+")
    file1 = open("Moradores.txt", "r", encoding="utf-8")
    lerFile = file1.read()
    mName = input("Nome: ")
    if mName in lerFile:
        while True:
            print("O nome ja existe, tente outro")
            mName = input("Nome: ")
            if mName not in lerFile:
                break
    mEmail = input("Email: ")
    mPass = input("Senha: ")
    file.write(mName + "|")
    file.write(mEmail + "|")
    file.write(mPass + "|")
    file.write("\n")
    print("Continue registering?")
    print("1 - YES || 2 - NO")
    choice = input()
    if choice == '2':
        break
    elif choice != '1':
        print("Invalid option")
        print("Continue registering?")
        print("1 - YES || 2 - NO")
        choice = input()
        file.close()
2
  • 3
    Just write def your_function(): at the start? Commented Nov 28, 2019 at 16:28
  • 2
    It depends on why you want this as a function. It is most of the time because we want to parameterize the code. What do you want as parameters? Commented Nov 28, 2019 at 16:30

2 Answers 2

2

You have declared the body of the function, you are just missing the function declaration for the most simple case (i.e. the one without arguments):

def do_something():
    while True:
        file = open("Moradores.txt", "a+")
        file1 = open("Moradores.txt", "r", encoding="utf-8")
        lerFile = file1.read()
        mName = input("Nome: ")
        if mName in lerFile:
            while True:
                print("O nome ja existe, tente outro")
                mName = input("Nome: ")
                if mName not in lerFile:
                    break
        mEmail = input("Email: ")
        mPass = input("Senha: ")
        file.write(mName + "|")
        file.write(mEmail + "|")
        file.write(mPass + "|")
        file.write("\n")
        print("Continue registering?")
        print("1 - YES || 2 - NO")
        choice = input()
        if choice == '2':
            break
        elif choice != '1':
            print("Invalid option")
            print("Continue registering?")
            print("1 - YES || 2 - NO")
            choice = input()
            file.close()

If you need additional arguments, declare it in the function declaration in this way:

def do_something(arg1, arg2, ...):

The simplest declaration which fits your code is this

def do_something():

(of course you can change the name of the function)

You can read more on functions here.

Sign up to request clarification or add additional context in comments.

Comments

1

Just write your code like below, you've to just add a function declaration line:

def function_name():
    #then your code here with proper indentation

#calling your function to perform defined task
function_name()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.