I tried putting a period after my variable name to make it a float so my result would be a float, but it gave an error. Why is this?
Answer
While it’s perfectly valid to make an integer a float by typing a period after the number, we cannot do the same to a variable, even though an integer is stored inside of it. If you try doing something like cucumbers. it will give you an error that says invalid syntax because a period after a variable name does not do what we want in this case.
Instead, we make use of the handy, built-in float() function! You can use it by writing any number or string between its parentheses, like so: float(cucumber)
…given that the price is set at 3.25. No finagling involved.
Aside
This is a subject that will have little meaning in a couple year’s time since Python 2.7 is going dark sometime after 2020. But for the meantime, it is still meaningful to have some guidelines when it comes to coercing floats.
On the command line this sort of shortcut is useful, but in code it is not. The more likely place to find a float in this rational is in the expression in the numerator. Maybe not in this instance but by-the-by in other dynamic situations. A counting number is certainly not a float and we should not entertain the fact that it is, or should be coerced to one just for expediency.
Is this right? Ive never seen how to use the one in brackets and even if the instruction says its right Im not entirely sure since you guys are using () thing
When a number literal is followed immediately by a period (dot) it is represented as a float for evaluation purposes.
>>> print (100.)
100.0
>>>
However, a variable is not a number literal, but a reference pointer to a value stored in memory. We are not acting directly upon the number, but on the reference, which will raise a Syntax error…
It’s a very badly written exercise in my opinion. The only time it even mentions Float() is in the hints and at the community forums section.
The actual exercise makes no mention of it despite it being what they’re trying to get you to do.
This seems to be the solution it’s trying to get from you but nothing points to that.
Simple is better than complex, so write for meaning, with purpose fairly clear and evident. As the programmer, you control the inputs and the output. How one composes the code is arbitrary, based on desired outcome.
As for setting both to float, that would not be necessary, ever. If the number in the denominator is a counting number, the it should rightfully be left as a counting number (integer).
Decimal fractions are not natural to binary, so computers implement a special brand of math called floating point arithmetic. It is from this term that we get the word, float.
No. There is no such term as floating decimals. Floating point arithmetic is very complex. The computer has a special chip called the math co-processor, or MPU to deal with number operations.
The closer term to describe the type of numbers, is Real. Floats are Real numbers.
adding machines have a switch option for something like ⅝, floating, and a couple of other options. I always took the “floating” to mean you could key 1.1 + 2.12 and the machine could line up the decimals correctly without the user having to key 1.10 to make it add correctly.
ok. You lost me on that last bit. the 5/4s makes sense. It’s amazing what you can forget over a period of a few years (ok. more like 40 years…). and look what I found:
What is floating decimal mode?
Definition of floating decimal. : a system of decimal point placement in an electronic calculator in which the decimal point is free to move automatically across the display in order to allow the maximum number of decimal places in the readout.
Floating Decimal | Definition of Floating Decimal by Merriam-Webster https://www.merriam-webster.com/dictionary/floating%20decimal
The key word is electronic which incorporates an LED display. Later on they went to crystal displays with pixels. In either case, they were more flexible for users than adding machines with only a paper tape.
So the term does exist (my bad) but does not apply, here. Google floating point arithmetic for a richer explanation. Expect it to be a tough read.