Search code examples
pythonraw-input

How to write a repeatable raw_input?


So I'm trying to write a script that allows a user to write down notes under different categories and then have those notes print to an output file. Heres a look at some example code.

def notes():
    global text
    text = raw_input("\nPlease enter any notes.\n>>> ")
    print "\Note added to report."
    notes_menu()

def print_note():
    new_report.write("\nNotes: \n%r" % text)

My question is in two parts:

  1. What can I use to make this so that if the notes method gets called again(with text already being assigned to a string) it creates a new variable called text1 and will keep doing so as many times as the notes method is called and text is assigned?

  2. How can I get the print method to keep checking, and printing as many text^nths as exist?


Solution

  • You should use lists for that:

    texts = []
    def notes():
        global texts
        txt = raw_input("\nPlease enter any notes.\n>>> ")
        texts.append(txt) # Add the entered text inside the list
        print "\Note added to report."
        notes_menu()
    
    def print_note():
        for txt in texts:
            new_report.write("\nNotes: \n%r" % txt)
    

    I hope that is what you wanted.

    EDIT: Since I am pretty sure I got downvoted because I used global, I would like to clarify: I used global because OP used global, not because that is a good solution.