6

I am new to Python and a student, my college has chosen the worst book on earth for our course. I cannot find examples of any concepts, so I apologize in advance as I know these concepts are very basic. I hope you can help me.

I need to know how to use the round feature in a string. I find examples but they do not show the string, just simple numbers.

Here is what we are supposed to get as an output: Enter the gross income: 12345.67 Enter the number of dependents: 1

The income tax is $-130.87 <---- this is what we are supposed to figure out

Here is the coding we are given to alter:

TAX_RATE = 0.20
STANDARD_DEDUCTION = 10000.0
DEPENDENT_DEDUCTION = 3000.0

# Request the inputs
grossIncome = float(input("Enter the gross income: "))
numDependents = int(input("Enter the number of dependents: "))   

# Compute the income tax
taxableIncome = grossIncome - STANDARD_DEDUCTION - \
                DEPENDENT_DEDUCTION * numDependents
incomeTax = taxableIncome * TAX_RATE 

# Display the income tax

print("The income tax is $" + str(incomeTax))

As I do not have an NUMBER to plug into the formula - I have to figure out how to use "incomeTax" - I have no idea how to do this. THe book doesnt explain it. Help?

1
  • incomeTax holds a (float) number. What you want is to format() it as string. Look up format() in the Python docs. Commented Jan 27, 2019 at 2:27

3 Answers 3

7

You can use format strings:

print("The income tax is ${:.2f}".format(incomeTax))

If you are using Python 3.6+, you can also use f-strings:

print(f"The income tax is ${incomeTax:.2f}")
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you - I am just starting out I have no clue what an F string is and I doubt this book will explain.... :(
@queenv PEP 498 describes the implementation, and Python 3's f-Strings: An Improved String Formatting Syntax (Guide) is a more approachable guide.
thank you I will add it to the list of alternate books :)
Short reminder that the dollar sign is part of the string and has nothing to do with the formatting. {incomeTax:.2f} is sufficient.
3

You can round just before making it a string:

print("The income tax is $" + str(round(incomeTax,2)))

Output:

Enter the gross income: 12345.67
Enter the number of dependents: 1
The income tax is $-130.87

4 Comments

This has the possibility of only showing one decimal place if incomeTax rounds to a tenth.
You rock!!!! That worked. It was also simple, imagine if I had been given an example to use! I do have a question for you: I notice that I am having a lot of trouble finding examples for concepts. I am googling with no results - am I using incorrect search terms? How can I get the examples I am searching for without having to post in here constantly?
@queenv Happy to help, don't forget to accept after timer is up, i actually think that you didn't understand the point, you were planning to round a string, but actually you can round it before it becomes a string, so searching "round a decimal python" would be it.
@queenv Lol, just saw it too.
-1

Im a student as well, with the same dumb book and HW.

I tried the above

str(round(incomeTax,2))

and it didn’t work. Maybe I typed something wrong. After playing around I found this to work

 # Display the income tax

incomeTax = round(incomeTax,2)

print(“The income tax is $” + str(incomeTax))

I hope this helps some other poor soul searching the web for an answer!

1 Comment

Kindly format your question according to the rules. Please use control+K to write your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.