I am trying to print the generated password into one line should get something like this 6yoCm&nN+UZj , instead one character is obtained, I switch to port 5000 and I see one character there, it changes when I refresh, and there needs to be a 12 line outlined characters as above. I tried to use return(random.choice(symbols), end='') , but it displays a syntax error. Ideas?

 from flask import Flask import random app = Flask(__name__) @app.route('/') def passwd_generation(): symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVYXWZ~$&_+-=/\\" for i in range(12): passwd = random.choice(symbols) return(passwd) if __name__ == '__main__': app.run() 

    1 answer 1

    random.choice(symbols) is the choice of one random symbol from symbols . If you execute passwd = random.choice(symbols) in a loop, each time the previous character will be overwritten. Line breaks have nothing to do with it.

    If you need a string of 12 characters, you need to add each character to passwd , and not to overwrite passwd :

     @app.route('/') def passwd_generation(): symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVYXWZ~$&_+-=/\\" passwd = '' # начальное значение - пустая строка for i in range(12): passwd += random.choice(symbols) # добавляем символ к строке return passwd 

    Regarding return(random.choice(symbols), end='') - this construction does not make sense. You probably borrowed end='' from a print(random.choice(symbols), end='') form print(random.choice(symbols), end='') , like this:

    1. end is a parameter to the print function, it will not work with return
    2. return is not a function, it is an operator, no brackets are required when using it

    Instead of a cycle with an accumulated value, you can use the generator:

     @app.route('/') def passwd_generation(): symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVYXWZ~$&_+-=/\\" passwd = ''.join(random.choice(symbols) for _ in range(12)) return passwd 

    You can read about such constructions, for example, here:
    Basics of Python - briefly. Part 4. List generators

    By the way, @app.route('/') is a function decorator , it refers to the function below, and not the code above, so it’s better to write it along with the function (as I have in the examples), and vice versa add empty lines above it (unless of course there is no other decorator).

    • thanks a lot!) - karaname