-2
first = input("first #:")
operator = input("operator: ")
second = input("second #:")


is_float = true ? false

if (is_float == true) {
 first = float()
 second = float()

} else if (is_float == false) {
  first = int()
  second = int()
}

print(first + operator + second )

I am practicing python and i want to make a more indepth calculator that has 2 numbers and various operators for adding subtracting multiplying etc and i want it to switch between a true or false boolean whether or not its a float number. im not quite sure how to do this would someone be willing to explain how booleans work in python? I completely understand not spoon feeding but im not sure how to finish this

  • 2
    `is_float = true ? false` What is this supposed to mean? Is this meant to be actual code? – John Gordon Apr 23 '23 at 18:53
  • You can check for the presence of a decimal point in the input string. If there is one, then you can assume the value is a float, otherwise you can assume integer. – John Gordon Apr 23 '23 at 18:56
  • @JohnGordon scientific notation may not have decimal point in it. [This answer](https://stackoverflow.com/questions/736043/checking-if-a-string-can-be-converted-to-float-in-python) describes correct way the best I guess. Although there is no clear question of what OP is struggling to achieve – sudden_appearance Apr 23 '23 at 19:01
  • 2
    Your first problem is that the code you are writing isn't Python. – mkrieger1 Apr 23 '23 at 19:02

2 Answers2

0

input() returns a string, so you can check for the presence of a decimal point.

If there is one, use float() to convert the input string, otherwise use int().

if '.' in first:
    first = float(first)

else:
    first = int(first)
John Gordon
  • 29,573
  • 7
  • 33
  • 58
-1

You could check if the floor value of the number differs from the number itself, giving is_float = (first - math.floor(first) < 0.00001), in this second case you will have to import the math package as import math at the top of your file.

But you will have to parse a number from the input like this:

first  = float(input("first #:"))
second = float(input("second #:"))

You cast the inputs to floats, and then chck if the floats are not just 'whole numbers'.

Jord van Eldik
  • 351
  • 2
  • 10
  • 2
    `is_float = isinstance(first, float)` This can't work, because it presumes the code has _already_ decided to call `int()` or `float()` on the user input string. But the question is -- how does the code _make_ that decision in the first place? – John Gordon Apr 23 '23 at 18:55
  • soryy you're right – Jord van Eldik Apr 23 '23 at 18:56