I am building a GUI BMI Calculator app using Python and tkinter and I need some help.
Since there must be no special characters in the user given string, I want an efficient way to detect special characters in a string.
I used regex compile method as I saw online. But it did not detect all the special characters. As when I gave 165,56 instead for 165.56, it returned and error in console. Other characters like #$@!$ etc, all worked perfectly showing tkMessageBox as I programmed.
I've tried each character in string iterating through and finding if special characters are present and I even used regex compile as mentioned above but none satisfies my queries.
regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]')
#assume have already got the value for h from user as 165,65
h = Entry.get(heightEntry)
if not h:
h = "0"
height = 0
elif h.isdigit() == True:
height = float(h)
elif h.isalnum() == True:
height = "None"
elif regex.search(h) == None:
for x in h:
if "." in x:
y += 1
if y >=2:
height = "None"
else:
height = float(h)
else:
height = "None"
#Check height lies between 0.5m and 3.0m or 50cm to 300cm
if not height:
print("There is no value for height to calculate")
tkMessageBox.showerror("Alert","No height value")
else:
if height == "None":
print("Invalid height value")
tkMessageBox.showerror("Alert","Invalid height value")
height = 0
elif not ((50.0<=height<=300.0) or (0.5<=height<=3.0)):
print("Invalid height value",height)
tkMessageBox.showerror("Alert","Invalid height value")
else:
if 50.0<=height<=300.0:
print("Height:",height,"cm")
else:
print("Height:",height,"m")
I expected the outcome to show the message box below height == "None"
But it showing traceback most recent call error.