I need to count the number of decimal places in a user-inputted float so that my code will not accept any number with more or less than 2 decimal places (the number is supposed to be a price). Here is my code:
while(flag == True):
try:
price = input("Enter the price: ")
price = float(price)
flag = False
#price must be positive
if float(price) < 0:
print("Invalid input for the new price.")
flag = True
#check for 2 decimal places
decimal = str(price)
decimal = decimal.split(".")
if len(decimal[1]) != 2:
print("Invalid input for the new price.")
flag = True
As of now it is working, except if I enter a price like 6.50, where the last decimal place is a 0. For some reason it is not accepting any number that ends in 0 as 2 decimal places. How can I fix this?