1

I am trying to keep a record of transactions into an excel file. The user is able to enter text via a textbox that I created using tkinter.

Here is a simplified version of my code.

quantity = '0'
newWindow = Tk()
buyLabel = Label(newWindow, text = 'How many apples do you want to buy?')
global textentry2
textentry2 = Entry(newWindow, width=10)
global quantity
if quantity=="":
   quantity=0
quantity = textentry2.get()
quantity = int(float(quantity))

I want my program to check if the value the user enters is acceptable. For the number of apples they buy, they should return an int bigger than 0. Therefore, return TRUE if the text entered in 'textentry2' is an int and bigger than 0 and FALSE if it isn't an int or smaller than zero. However, I do not know how to check if the entered text is an int, so I tried to convert it to an int first then just check if it is > 0. HERE IS MY CODE:

if (quantity > 0):
     print('your value is stored')
else:
     print('please enter an acceptable quantity')

I got an error that said invalid literal for int() with base 10: ' ' and from another post in stackoverflow, they said to convert the variable to float, then int, and this is why my code looks like this:

quantity = int(float(quantity))

however, I still get an error 'ValueError: could not convert string to float:' I AM CONFUSED. can anyone help? It would also be great if you know how to check if the entered text is an int.

4
  • Why do you want to convert it to a float? You can just directly convert it to an int() Commented Mar 2, 2021 at 11:52
  • 2
    try to use .isnumeric() str method that returns True if string is numeric. And then if it is True just check that it is not less than -1 Commented Mar 2, 2021 at 11:55
  • when I directly converted to int there was an error: invalid literal for int() with base 10: ' '. Commented Mar 2, 2021 at 11:57
  • That means it is not an int. The usual way to cope with that is to catch the exception. Commented Mar 2, 2021 at 11:58

2 Answers 2

2

As I know, you can just convert it to an int with int(quantity). Before that, you should check if the string is numeric with quantity.isnumeric().

So with your code it should look something like this:

quantity = '0'
newWindow = Tk()
buyLabel = Label(newWindow, text = 'How many apples do you want to buy?')
global textentry2
textentry2 = Entry(newWindow, width=10)
global quantity

quantity = textentry2.get()
if quantity.isnumeric():                               # Check if the quantity is a number, as TheLizzard said, negative numbers in a string will be marked as not numeric so no need to check if it is greater than 0
   quantity = int(quantity)                            # Then set the quantity to the converted int
else                                                   # If it's not
   # whatever you want to do if it is not
Sign up to request clarification or add additional context in comments.

4 Comments

btw "-4".isnumeric() returns False. So there is no point to the int(quantity) > 0 in your code
but negative numbers aren't wanted anyways as I understood
I am saying that it is useless code. It will only be executed when quantity.isnumeric() is True which only happens when the numbers are +ve so technically if you remove the ` and int(quantity) > 0` the code will run slightly faster.
first of all thanks! quantity.isnumeric() is still useful to check if the value is acceptable (if they enter a str for example I can print 'please enter a valid quantity!' :)
1

You can use python built-in string's method .isnumeric() like this:

>>> "".isnumeric()
False
>>> "a".isnumeric()
False
>>> "5".isnumeric()
True
>>> "55".isnumeric()
True
>>> "55a".isnumeric()
False

but there might be a problem if you also want to check if the number is negative because:

>>> "-5".isnumeric()
False
>>> a = "-4"
>>> a.lstrip("-").isnumeric() and (len(a)-len(a.lstrip("-")) <= 1)
True
>>> a = "--4"
>>> a.lstrip("-").isnumeric() and (len(a)-len(a.lstrip("-")) <= 1)
False

The send part (len(a)-len(a.lstrip("-")) <= 1) checks if there is more than 1 "-"s before the number.

2 Comments

thanks! this is helpful. Negative values are not wanted anyways for this code so it actually is even better!
@JingchunHe I added negatives for completeness and other people might find this page later might also want negatives

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.