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()
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()
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()
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.)
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.
PROBLEM WITH PYTHON PLEASE HELP ME/ PROBLEM MIT PYTHON BITTE HELFEN MICH 05/26/2017 - Metin2 Private Server - 0 Replies What are the values to change to move the search for the bonus in the item that is in the inventory's first slot?
I would like to move it to the second slot (python switcher)
Was sind die Werte, die geändert werden sollen, um die Suche nach dem Bonus in dem Element zu verschieben, der sich im ersten Slot des Inventars befindet?
Ich möchte es auf den zweiten Slot (Python Switcher)
[Python-Modul]EXP-Donator (kompatibel mit Python Loader) 11/23/2013 - Metin2 Hacks, Bots, Cheats, Exploits & Macros - 27 Replies Moin,
da man mich danach gefragt hat und ich sowieso mal ein Beispiel für die Benutzung meines Python Loaders veröffentlichen wollte, habe ich die Gelegenheit genutzt und euch eben einen EXP-Spendebot geschrieben.
Man kann ihn einfach mit dem oben verlinkten Python Module Loader laden und ihn mit F5 aktivieren/deaktivieren.
Sobald ihr mehr als 99 Erfahrungspunkte habt (man kann nur in 100er Schritten spenden), werden alle Erfahrungspunkte an eure Gilde gespendet.
Wer Lust hat und...