Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > General Coding
You last visited: Today at 19:02

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Need Python Help

Discussion on Need Python Help within the General Coding forum part of the Coders Den category.

Reply
 
Old   #1
 
sick of love songs's Avatar
 
elite*gold: 0
Join Date: Jul 2019
Posts: 35
Received Thanks: 3
Arrow Need Python Help

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.
sick of love songs is offline  
Old 07/28/2019, 11:03   #2

 
elite*gold: 64
Join Date: May 2011
Posts: 1,229
Received Thanks: 854
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)
BladeTiger12 is offline  
Old 07/28/2019, 11:09   #3
 
sick of love songs's Avatar
 
elite*gold: 0
Join Date: Jul 2019
Posts: 35
Received Thanks: 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
sick of love songs is offline  
Old 07/28/2019, 11:40   #4

 
elite*gold: 64
Join Date: May 2011
Posts: 1,229
Received Thanks: 854
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()
BladeTiger12 is offline  
Thanks
1 User
Old 07/28/2019, 12:19   #5
 
sick of love songs's Avatar
 
elite*gold: 0
Join Date: Jul 2019
Posts: 35
Received Thanks: 3
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
sick of love songs is offline  
Old 07/28/2019, 13:20   #6
 
sk8land​'s Avatar
 
elite*gold: 50
Join Date: Nov 2018
Posts: 1,065
Received Thanks: 2,594
Just do
Code:
codes = '\n'.join([''.join(random.choices(chars, k=length)) for _ in range(number)])
sk8land​ is offline  
Old 07/28/2019, 15:27   #7

 
elite*gold: 64
Join Date: May 2011
Posts: 1,229
Received Thanks: 854
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.
BladeTiger12 is offline  
Old 07/28/2019, 15:58   #8
 
elite*gold: 46
Join Date: Oct 2010
Posts: 782
Received Thanks: 525
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.
th0rex is offline  
Old 07/28/2019, 16:52   #9

 
elite*gold: 64
Join Date: May 2011
Posts: 1,229
Received Thanks: 854
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.)
BladeTiger12 is offline  
Old 10/24/2019, 19:05   #10
 
elite*gold: 0
Join Date: Dec 2016
Posts: 14
Received Thanks: 0
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.
Attached Images
File Type: png python.PNG (13.3 KB, 20 views)
LinKVoltz is offline  
Reply


Similar Threads Similar Threads
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...
Help to make a python file works with python loader
03/03/2013 - Metin2 - 2 Replies
Hey epvp! I want make a very. Little hack works on pythonn loader can anybody help me please?
Metin2 - Python - Wie Python Hacks verschlüsseln und Server überprüfen (GF/PServe)
09/23/2012 - Metin2 - 2 Replies
Ich wollte fragen, wie man Python Hacks am besten Verschlüsselt ? und wie man feststellen kann ob man auf einem GF / Pserver spielt. ?
Python + Eric Python IDE installieren ?!
07/05/2011 - General Coding - 0 Replies
hat sich erledigt.



All times are GMT +1. The time now is 19:03.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.