Tip calculator returns zeros [duplicate] - python

This question already has answers here:
What is the difference between '/' and '//' when used for division?
(16 answers)
Closed 6 years ago.
I'm having some trouble running the code I wrote.
meal = float(raw_input("How much did your meal cost? "))
tax = 6.75 / 100
tip = 15 / 100
total = (meal * tax) * tip
print total
You can see above that I made this "tip calculator". Whenever I enter a number, it returns with zeros. It seems like it's skipping the entire calculation part.
Any solution?

If it is python 2.7 you are doing integer division, which means your tip is calculating as 15/100 = 0.
from __future__ import division
At the the top of the program will solve this.

In python 2, / is integer division. Thus, 15 / 100 = 0, and you miltiply by that. Just replace 15 with 15.0

The problem is on this line:
tip = 15 / 100
Dividing an int by an int will result in an int. In this case 15 / 100 gives you 0, and the complementary modulo operation 15 % 100 gives the 15 remainder.
If you change 15 to a float it will work because precision is preserved and tip will be a float instead of an int. Try:
tip = 15.0 / 100

Related

Round number down to the next 1000 in python [duplicate]

This question already has answers here:
Python: how can I round a number downward to next 1000
(4 answers)
Closed 10 months ago.
can somebody tell me how i can round down to the nearest thousand. So far I tried it with math.round(), the truncate function but i couldn't find my math skills to work out for me. As a example for some people I want that 4520 ends up in beeing 4000.
In Python, you can do
print((number // 1000)*1000)
just a thought
Why not do it the traditional way?!
deprecate_vals = 3
val = 4520
val = int(val/(10**deprecate_vals)) * (10**deprecate_vals)
print(val)
You can divide by 1000 before rounding and then multiply by 1000.
invalue = 4320
outvalue = 1000 * round(invalue/1000)
print("invalue: ",invalue)
print("rounded value: ",outvalue)
output
invalue: 4320
rounded value: 4000
There is multiple way to do it.
You can get the digit for the thousand by using the division (// mean without remaining, so you don't get 4,52 but 4)
x = 4520
rounded_x = (x//1000) * 1000
You can also use the remaining of the division with the modulo operator and remove it to the value :
x = 4520
rounded_x = x - (x%1000)

How to round to two decimal places in python? [duplicate]

This question already has answers here:
How to round to 2 decimals with Python? [duplicate]
(21 answers)
Closed last year.
Im trying to calculate a bill in python for hw. Cant figure out how to round the numbers to two decimal places. Here's what I have so far. Every time I try the round function, it doesnt work and gives me an error message. Help?!
ss_cost = 3.95 * 2
hb_cost = 8.95 * 2
ds_cost = 2.50 * 2
subtotal = (ss_cost) + (hb_cost) + (ds_cost)
tax = (round(subtotal * 0.0475), %.2)
print (tax)`
You missplaced the %.2:
tax = (round(subtotal * 0.0475, 2))
And you don't need the %.
round(number[, ndigits])
Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.
For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise, the return value has the same type as number.
When it comes to amounts, you shouldn't use round, it's better to use decimal.
from decimal import Decimal
ss_cost = 3.95 * 2
hb_cost = 8.95 * 2
ds_cost = 2.50 * 2
subtotal = (ss_cost) + (hb_cost) + (ds_cost)
tax = Decimal(subtotal * 0.0475).quantize(Decimal("0.00"))
print (tax)
# 1.46

Terminal not printing python code on Mac [duplicate]

This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 5 years ago.
I made a small python file on my desktop for fun, and I'm frustrated to find that no values are being returned. Here is the code from the file:
def calc(x):
chance =1
for i in range(1,x+1):
chance *= ((365-i)/365)
return chance * 100
string = calc(23)
print string
And here is what I type into the terminal:
cd Desktop #where file is located
python py.py #py is the name of the file
And this is what it returns:
0
Since 365 and i are integers, you are performing integer division, which only returns the "whole" part of the result. Since 365-i is lesser than 365 dividing the two will result in 0, and continuously multiplying it by anything will still give 0.
You could use the 365.0 notation to use floating point division, which will eventually generate the result 46.1655742085:
chance = 1
for i in range(1,x+1):
chance *= ((365.0 - i) / 365.0)
# Floating point^-----------^
Well so on your first iteration your i = 1. 365-1 = 364. 364/365 = 0 because it is integer division in python. So the answer will always give you 0 unless you change it to allow for you to give a float.
Changing 365 to 365.0 will give you your answer
chance = chance * ((365-i)/365.0)
I just changed the 365 to 365.0 and I got
2.23071139399e-05
As my output
The result is correct in Python2.x. When a/b and a is less than b, result is 0.
Modify 365 to 365.0 it will cast the division result to float
chance *= ((365-i)/365.0)

Division error in python, result is incorrectly rounded [duplicate]

This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 8 years ago.
I am trying to make a BMI calculator function. I am learning python at pyschools.
This is my code:
# Note: Return a string of 1 decimal place.
def BMI(weight, height):
x = weight /(height*height)
g = round(x,1)
return g
And pyschools shows me that these are the right answers:
With 110 = weight and 2 = height I am supposed to get a BMI of 27,5.
But I instead get 27.
Then it does a second check to make sure I wrote the code right and tells me 24,2 is the right answer but my program did return 24,2. But it still marks my answer in red and says "my" 24,2 is wrong and the website's is right.
If someone has a better site or anything to learn python it would also be appreciated since this website seems to be kind of wrong sometimes. And I am looking for free online resources. No books please.
To fix it for all cases, add this line to the top:
from __future__ import division # Make division work like in Python 3.
in Python 2, / means integer division.
With this in mind, in Python 2 if you pass intgers into division, it will give you an integer back. Anything that would have been a float is floored*. Therefore another option to get the desired result is to pass a float in, so instead of:
weight / (height*height)
do:
float(weight) / (height*height) # float in means float out.
*This means that only full times that the divisor goes in are counted. So 1/2 will get 0 because 2 goes fully into 1 0 times.
def BMI(weight, height):
x = float(weight) /(height*height)
g = round(x,1)
return g
see Python division
and Binary arithmetic operations
The issue lies with your division.
Division as we intrinsically know it is floating point division, or division where 1 / 2 evaluates to a fraction, 0.5. In standard programatic division, the 1, 2 are ints() and therefore cant be fractions, or floats() as the type is called in python. The expression, 1 / 2 therefore evaluates as 0, as 2 as a whole integer cant go into one entirely any times.
Ex:
In [1]: 1 / 2
Out[1]: 0
# Explicitly what is going on, since 1 and 2 are ints.
In [2]: int(1) / int(2)
Out[2]: 0
#fixed with floating division
In [3]: float(1) / float(2)
Out[3]: 0.5
# protip: only one of the divisors needs to be a float for python to divide correctly.
In [4]: 1 / float(2)
Out[4]: 0.5
Use x = weight / float((height*height)) to get the results you expect.
# Note: Return a string of 1 decimal place.
def BMI(weight, height):
x = weight / float((height*height))
g = round(x,1)
return g

Want to seperate the integer part and fractional part of float number in python [duplicate]

This question already has answers here:
in python, how do i split a number by the decimal point
(8 answers)
Closed 9 years ago.
I am Working on Converting the value of foot to inches where 1 foot = 12 inches. I am calculating the height of person in inches. ex. 5.11 a person of height 5 foots and 11 inches means total 71 inches. Is there any way in Python so i can separate the int part & float part of float number for further calculations ? any suggestions are welcome.
To get the integer part of a float use the built-in int() function:
>>>int(5.1)
5
To separate the float part subtract the float with the integer:
>>>5.1 - int(5.1)
0.1
Or you could get the modulus (which is the float part) of the float with 1:
>>> 5.1 % 1
0.09999999999999964 #use the round() function if you want 0.1
For you, 5.11 is not a float. If it were, then it would mean 5.11 feet, which is 61.32 inches.
5.11 is a string containing two pieces of data and a separator - parse it like one! If you change the separator to the more conventional ' (i.e. 5'11), it becomes obvious it is not a single float:
raw = raw_input("Enter feet'inches")
feet, inches = map(int, raw.split("'", 1))
Another way is to use the divmod (function or operator), using as denominator (divisor) the number 1:
>>> divmod(5.11, 1)
(5.0, 0.11000000000000032)
>>> 5.11.__divmod__(1.)
(5.0, 0.11000000000000032)

Categories

Resources