Need Python Help

07/28/2019 10:46 sick of love songs#1
Since yesterday I started to learn python and today I tried to write my first "programm". But I have one problem, I want the programm to save all the generated codes into one .txt file but when its finished it just saves the last code (e.g. 50/50 codes generated but only the last one is saved in the text file).


Thats my code:

Code:
import random

chars = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")

length = 16
number = input("How many codes?")
number = int(number)

for p in range(number):
    code = (" ")
    for c in range(length):
        code += random.choice(chars)
    print(code) 

file = open("codes.txt","w")
file.write(code)
file.close()
Pls help me I cant find a solution in google.
07/28/2019 11:03 BladeTiger12#2
It's because you reset your string to space in this line:

code = (" ")

Change it to:

code += (" ")

BTW: Never wrote python, dunno how to add new line. (Simple \r\n? or is there any macro)
07/28/2019 11:09 sick of love songs#3
Quote:
Originally Posted by BladeTiger12 View Post
It's because you reset your string to space in this line:

code = (" ")

Change it to:

code += (" ")

BTW: Never wrote python, dunno how to add new line. (Simple \r\n? or is there any macro)
Thank you but if I change it to "code += (" ")" it comes an error.

New line is \n
07/28/2019 11:40 BladeTiger12#4
I guess you need to declare it before you can concatenate.
Try it like this:
(Declare your code variable before, and at the end of every code generation add a new line)

HTML Code:
import random

chars = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")

length = 16
number = input("How many codes?")
number = int(number)
code = ("")

for p in range(number):
    for c in range(length):
        code += random.choice(chars)
    print(code)
    code += "\n"

file = open("codes.txt","w")
file.write(code)
file.close()
07/28/2019 12:19 sick of love songs#5
Quote:
Originally Posted by BladeTiger12 View Post
I guess you need to declare it before you can concatenate.
Try it like this:
(Declare your code variable before, and at the end of every code generation add a new line)

HTML Code:
import random

chars = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")

length = 16
number = input("How many codes?")
number = int(number)
code = ("")

for p in range(number):
    for c in range(length):
        code += random.choice(chars)
    print(code)
    code += "\n"

file = open("codes.txt","w")
file.write(code)
file.close()
Now it works. Thank you very much! <3
07/28/2019 13:20 sk8land​#6
Just do
Code:
codes = '\n'.join([''.join(random.choices(chars, k=length)) for _ in range(number)])
07/28/2019 15:27 BladeTiger12#7
Quote:
Originally Posted by sk8land​ View Post
Just do
Code:
codes = '\n'.join([''.join(random.choices(chars, k=length)) for _ in range(number)])
I guess bad idea for beginners...
Totally unreadable.
07/28/2019 15:58 th0rex#8
Quote:
Originally Posted by sk8land​ View Post
Just do
Code:
codes = '\n'.join([''.join(random.choices(chars, k=length)) for _ in range(number)])
BTW, the [] are not needed in this case because the list comprehension is used as an argument for a function call.
Code:
codes = '\n'.join(''.join(random.choices(chars, k=length)) for _ in range(number))
works as well.

Also idk whats more readable than this, doing it manually and over 5 lines certainly isn't.
07/28/2019 16:52 BladeTiger12#9
Quote:
Originally Posted by th0rex View Post
BTW, the [] are not needed in this case because the list comprehension is used as an argument for a function call.
Code:
codes = '\n'.join(''.join(random.choices(chars, k=length)) for _ in range(number))
works as well.

Also idk whats more readable than this, doing it manually and over 5 lines certainly isn't.
Sure I said for "beginners".
I don't know how experienced u are, but also in other languages it's the same. Sure u can always optimize ands shorten everything, but it's useless if you try to learn. (For me it feels like it's hard to read for a newbie.)
10/24/2019 19:05 LinKVoltz#10
You also could have just moved the "file.write(code+'\n')" also add the "+'\n'" at the end of "code" for a new line so it isnt all jumbled up in one. in with the "for p in range(number):" loop and then moved the initial statement of "file = open("codes.txt","w"" above the first for loop. What you have here is that every time the for loops goes through it rewrites what was previously written. i have attached an image of the code to help :)


Everybody else's explanation here was so extra when all he had to do was move the code of when he opened the file.