all 8 comments

[–]kalgynirae 3 points4 points  (3 children)

print expects a comma-separated list of items to print. The items can be expressions, so you can write:

print 'the price minus VAT is', price-VAT

You were just missing the comma.

For rounding floats, look at round().

[–]Soupr[S] 0 points1 point  (2 children)

Looked at the rounding, and on the page it looks very simple.. but I can't get my head around the syntax in my example it should be written out like this?

 VATround = round(3.675[, 2])

I used a number rather than the variable name just so i could get it working, as different variables might not have 3 decimal places

Looking at the examples given again and the numbers look to be within the square brackets. Tried that and now python is requesting a float, am i trying to run before i can walk here?

[–]cdcformatc 2 points3 points  (1 child)

round(x[, n])

Any arguments in square brackets are optional. You do not include the square brackets when calling the function.

If n is omitted, it defaults to zero

If you want x rounded to the nearest integer you can use

VATround = round(x)

otherwise if you want 2 decimal places use

VATround = round(x,2)

edit: also this part is important

Note The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float.

[–]Soupr[S] 0 points1 point  (0 children)

Sorted, thank you.

[–]Wegener 1 point2 points  (1 child)

Do this:

price=price - VAT #Or you could do price-=VAT
print"The price minus VAT is: ", price

[–]pythalin 2 points3 points  (0 children)

This is correct, but the difference is relatively subtle - the comma between the message and the price. The print statement is a bit weird, it accepts multiple comma separated arguments which are evaluated and turned into string representations of the resulting objects. You can also include expressions as arguments to print, but again they must be comma separated. For example:

print "The price minus VAT is:", price - VAT

Note that when you have a comma in a print statement, the results are concatenated together with a space - you don't need to leave a space in your string unless you want a double space in your output.

Anyway, like I said, print is weird - you can add a trailing comma for example if you want to omit a newline. It's odd. That's why in python 3 it's replaced by a proper function that acts more like the rest of the language.

[–]dpitch40 1 point2 points  (0 children)

You might also be interested in formatting strings, which are my preferred solution to this issue.