-1

I have this code. I want to use floats as an option for a calculator, when I run the code and attempt to enter my first value as a decimal, it throws me this error message invalid literal for int() with base 10: '3.4'

#loop
while True:
    input_mult = "*"
    input_div = "/"
    input_add = "+"
    input_sub = "-"
    first = input('Please enter your first value: ')
    one = int(first)
    if one is float:
        continue
    onef = float(first)
    operator = input('Please enter your operator: ')
    second = input('Please enter your second value: ')
    twof = float(second)
    two = int(second)
    if operator == input_mult:
        print(one * two)
    if operator == input_div:
        print(one / two)
    if operator == input_add:
        print(one + two) 
    if operator == input_sub:
        print (one - two)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
NotM1zzel
  • 1
  • 1
  • 3
    Can't you just make it use floats for everything? Why do you need integers for a calculator? – Mark Sep 18 '22 at 21:33
  • In general, if you want to try to convert a string to int but continue if the conversion fails, you need to wrap the int conversion into a try/except block and put the logic you need for when conversion failed in the except block. – joanis Sep 18 '22 at 21:44
  • Does this answer your question? [Converting String to Int using try/except in Python](https://stackoverflow.com/questions/8075877/converting-string-to-int-using-try-except-in-python) – joanis Sep 18 '22 at 21:45

1 Answers1

0

Look up the help for the int() function, which opens with:

Convert a number or string to an integer, or return 0 if no arguments are given.

i.e. the input to int() needs to be a number, so convert it first:

one = int(float(first))  

i.e. the int() function doesn't change change the type of a variable: it performs an operation upon it too (i.e. rounding if required). Rounding isn't defined for a string variable, so that first needs to be converted to a float before int() can operate on it.

This will then free you up to experience further issues, such as this line not being great Python:

if one is float:

That needs to be something like:

if type(one) is float:

Python is a dynamic language, but it still does really care about types: they need to match expectations. When it comes to dealing with numbers, this isn't JavaScript.

Having said that, it isn't really clear what your logic is intending here. e.g. First you create one as an int, and then immediately test to see if it is a float? This script needs more critical evaluation.

Michael MacAskill
  • 2,411
  • 1
  • 16
  • 28
  • Struggling to see what this accomplishes: `one = int(float(first))` if the user types in `2.5` the calculator will treat is as `2` which can't be what is intended for a calculator. – Mark Sep 18 '22 at 21:50
  • @Mark Agree entirely - am just showing how to address the error the OP has seen - as noted, the script itself doesn't seem to be well-conceived. Thinking more about the types of variables will hopefully help them make progress to something more sensible. – Michael MacAskill Sep 18 '22 at 21:52