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.

  • Why do you want to convert it to a float? You can just directly convert it to an `int()` – kuso.sama Mar 02 '21 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``` – crackanddie Mar 02 '21 at 11:55
  • when I directly converted to int there was an error: invalid literal for int() with base 10: ' '. – Jingchun He Mar 02 '21 at 11:57
  • That means it is not an `int`. The usual way to cope with that is to catch the exception. – tripleee Mar 02 '21 at 11:58

2 Answers2

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
kuso.sama
  • 336
  • 1
  • 9
  • btw `"-4".isnumeric()` returns `False`. So there is no point to the `int(quantity) > 0` in your code – TheLizzard Mar 02 '21 at 12:04
  • 1
    but negative numbers aren't wanted anyways as I understood – kuso.sama Mar 02 '21 at 12:13
  • 2
    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. – TheLizzard Mar 02 '21 at 12:16
  • 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!' :) – Jingchun He Mar 02 '21 at 12:18
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.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • thanks! this is helpful. Negative values are not wanted anyways for this code so it actually is even better! – Jingchun He Mar 02 '21 at 12:20
  • 1
    @JingchunHe I added negatives for completeness and other people might find this page later might also want negatives – TheLizzard Mar 02 '21 at 12:22