D
D
Desmoke2021-03-14 14:32:04
Python
Desmoke, 2021-03-14 14:32:04

The problem with the Generator, how to fix the error?

Greetings to those who read this question.
Could you help me?
So, I wrote a generator with gui, but there is a problem,
It does not write the required number of codes in txt.
Here is the form:
604df2ac433bd064404279.png
*Above and below lineEdits, in the middle pushButton

If you enter some number, then this number should be generated and written to txt.
But no matter how, when you click on the generate button, only 1 code is generated and written.

Here is the code itself:

class PMain(QtWidgets.QMainWindow):
    def __init__(self):
        super(PMain, self).__init__()
        uic.loadUi("interface.ui", self)
        self.setWindowTitle("Генератор Дискорд Нитро")
        self.setWindowIcon(QtGui.QIcon("`pwd`/icon.ico"))

        if self.lineEdit_2.text() == "Введи количество желаемых кодов:":
            self.pushButton.clicked.connect(self.gen)
        else:
            self.pushButton.clicked.connect(self.how_much)

    def gen(self):
        f = open("code.txt", "a+")
        code = "https://discord.gift/" + ''.join(random.choices(string.ascii_letters + string.digits, k=16))
        f.write(f"{code}\n")
        f.close()
        self.lineEdit.setText(code)

    def how_much(self):
        n = int(self.lineEdit_2.text())
        value = 1
        while value <= n:
             code = "https://discord.gift/" + ('').join(random.choices(string.ascii_letters + string.digits, k=16))
             f = open('code.txt', "a+")
             f.write(f'{code}\n')
             f.close()
        value += 1


Help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MinTnt, 2021-03-14
@Desmoke

value += 1
is outside the loop, so the loop is infinite.

A
alexbprofit, 2021-03-14
@alexbprofit

def how_much(self):
        n = int(self.lineEdit_2.text())
        value = 1
        f = open('code.txt', "a+")
        while value <= n:
             code = "https://discord.gift/" + ('').join(random.choices(string.ascii_letters + string.digits, k=16))

             f.write(f'{code}\n')
             value += 1
       f.close()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question