Home > OS >  How can I get my calculator to accept decimal integers without throwing a ValueError?
How can I get my calculator to accept decimal integers without throwing a ValueError?

Time:06-18

So I have developed a basic calculator which uses the following code to get the input of the user:

first_value = int(input("Enter the first value: ")) # get the first value

However if I type in a number such as 1.5 or 1.115 or 1.1115 or -1.5; I will get a ValueError.

How can I get the calculator to work with decimals aswell?

CodePudding user response:

use,

float(input("Enter the first value: "))

CodePudding user response:

use float instead of int

first_value = float(input("Enter the first value: "))

CodePudding user response:

use Float and put it in a try block for better error handling

try:
  first_value = float(input("Enter the first value: "))
except:
  print("An exception occurred")
  • Related