there is no erors in this code but i cant understand the condition, if i change the condition to ‘
if money != string.digits:‘when i run the code with this condition if i put a number it will go to an endless loop until i insert anything except a digit wich i dont want but the condition is if its not equal the is statement will work.
the code you see is what i mean the condition is if the input from the user is a digit run the if statement but when i run the code it dont it does what i want,i know thats a hurry!
but i dont feel confident at all cus i cant get it thats all.
import string money=float(input("put you salary (year)")) while True: try: if money == string.digits: print("please enter a digit") money = float(input("put you salary (year)")) else: break except: money = float(input("put you salary (year)")) price = money*(2.5/100) print(price)
You can just try/except to convert the input to float.
while True: try: money = float(input("put you salary (year)")) break except ValueError: print("This is not a number. Please enter a valid number") price = money * (2.5 / 100) print(price)
Python mentions EAFP(easier to ask for forgiveness than permission) because it usually results in faster, cleaner code. This means you assume the input will be a float and just catch the error when it’s not. Instead of validating the input before the error.
Also, when dealing with float arithmetic, be sure to keep in mind the issues and limitations outlined here: https://docs.python.org/3.8/tutorial/floatingpoint.html