Issue
This Content is from Stack Overflow. Question asked by MMDRZA
I want to receive a data from the user in my program in the form of a number, but I don’t know why it gets an error
self.InputTotal = QtWidgets.QLineEdit(Form)
self.InputTotal.setGeometry(QtCore.QRect(60, 20, 141, 31))
self.InputTotal.setObjectName("InputTotal")
chkinput = self.InputTotal.text()
Need chkinput just number.
if int(chkinput) == 0:
ranger = 1000
else:
ranger = chkinput
for i in range(ranger):
self.btn_eth.clicked.connect(self.Generator)
Solution
The issue you are facing is that when you run if int(chkinput) == 0
all it does is check if the chkinput
variable cast to an integer equals zero. It doesn’t actually convert chkinput
value to an integer.
For that you would need to assign the converted value back to the variable name like: chkinput = int(chkinput)
.
So currently when your chkinput
doesn’t equal 0, you are assigning a string value to the ranger
variable in the else
clause which will throw an error when you try to use it as a range.
You should also add some extra error checking as well so you don’t run in to even more issues. For example, you can check that the text isn’t an empty string, then check if the contents is a number using the .isdigit()
method, then if that returns true then you know it is safe to use the int
constructor without it throwing an error.
chkinput = self.InputTotal.text()
if len(chkinput) > 0 and chkinput.strip().isdigit():
chkinput = int(chkinput)
if chkinput == 0:
ranger = 1000
else:
ranger = chkinput
for i in range(ranger):
self.btn_eth.clicked.connect(self.Generator)
This Question was asked in StackOverflow by MMDRZA and Answered by Alexander It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.