Reputation: 5
I'm begginer in writing applications in python. I have a problem with my simple application, that generates random number fro given range. It starts, without errors. i write in two numbers, click 'generuj' (generate in Polish), and then application stops responding. There are not any errors, it just freezes, like it have some infinite loop or something like that. Can some one tell me, why it's happening? Or at least, tell me how to get the information about the error from that situation? Here's the code:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from sys import argv
from random import randint
class OknoGeneratora(QDialog):
def __init__(self):
super(OknoGeneratora, self).__init__()
self.setWindowTitle("Generator losowych liczb") #random number generator
self.setFixedSize(QSize(320,240))
gm = QVBoxLayout()
gm.addStretch(1)
gm.addWidget(QLabel("Podaj początek i koniec zasięgu w jakim mają być generowane liczby (dodatnia i całkowita): ")) #give the range of numbers
self.__liczba1=QLineEdit()
self.__liczba2=QLineEdit()
gm.addWidget(self.__liczba1)
gm.addWidget(self.__liczba2)
gm.addStretch(1)
dm = QHBoxLayout()
self.__generuj = QPushButton("Generuj") #generate
dm.addWidget(self.__generuj)
self.__koniec = QPushButton("Zakończ") #close
dm.addWidget(self.__koniec)
gm.addLayout(dm)
self.__generuj.pressed.connect(self.generuj)
self.__koniec.pressed.connect(self.koniec)
self.setLayout(gm)
def generuj(self):
sprawdz=0
if self.__liczba1.text():
for zn in self.__liczba1.text():
if not zn.isdigit():
sprawdz=1
QMessageBox.critical(self, "Błąd", "Zły napis! Proszę podać liczbę.") #Error, Give the number
break
elif self.__liczba2.text():
for zn2 in self.__liczba2.text():
if not zn2.isdigit():
sprawdz=1
QMessageBox.critical(self, "Błąd", "Zły napis! Proszę podać liczbę.") #Error, Give the number
break
if sprawdz!=1:
li1 = int(self.__liczba1.text())
li2 = int(self.__liczba2.text())
wynik=""
while li1>0 and li2>0:
wynik=randint(li1,li2)
QMessageBox.information(self,"Ok!","Wygenerowana liczba to: " + wynik) #Ok, generated number is:
else: QMessageBox.critical(self,"Błąd","Zły napis! Proszę podać liczbę dodatnią.") #Give the number
def koniec(self): QCoreApplication.instance().quit()
app = QApplication(argv)
okk = OknoGeneratora()
okk.show()
app.exec_()
Upvotes: 0
Views: 74
Reputation: 85462
Look at this part:
while li1>0 and li2>0:
wynik=randint(li1,li2)
You never change the values of l1
or l2
. This means Python will call wynik=randint(li1,li2)
over and over again. You need to either change the values of l1
or l2
inside the loop or use break
in the loop at some point.
Did you mean this?
if li1 > 0 and li2 > 0:
wynik = randint(li1, li2)
Upvotes: 1