Python Forum
Problem with my code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Problem with my code (/thread-23431.html)



Problem with my code - Farcrafting - Dec-30-2019

Hello.

Iam new here in the forum.
I have a problem with an exercise and hope to get a few tipps and help.
First of all, I'm not a student. I do that for myself.



—————————————————————————————————————————-
The exercise:

You are getting ready to paint a piece of art. The canvas and brushes that you want to use will cost 40.00. Each color of paint that you buy is an additional 5.00. Determine how much money you will need based on the number of colors that you want to buy if tax at this store is 10%.

Task
Given the total number of colors of paint that you need, calculate and output the total cost of your project rounded up to the nearest whole number.

Input Format
An integer that represents the number of colors that you want to purchase for your project.

Output Format
A number that represents the cost of your purchase rounded up to the nearest whole number.

Sample Input
10

Sample Output
99


—-———————————————————————————————————————————————————————

My code:
inp = int(input())


def color(stk):
  colorprice = stk * 5
  price = colorprice + 40
  tax = (price * 10) / 100
  final = price + tax
  return round(int(final))
  
  
  
print(color(inp))
Whats wrong with that? It makes me stupid :D


RE: Problem with my code - ichabod801 - Dec-30-2019

I don't know. What is wrong with it? I input 10, I get 99.


RE: Problem with my code - Farcrafting - Dec-30-2019

Hello and Thanks for your answer !

I dont know ... thats the problem...
My learning tool tells me it would be wrong, the tool test it with 5 unknown Inputs. 2 of 5 cases would be wrong


RE: Problem with my code - Axel_Erfurt - Dec-30-2019

for example

inp = 3
 
 
def color(stk):
  colorprice = stk * 5 #  3*5=15
  price = colorprice + 40 # 15+40=55
  tax = (price * 10) / 100 # 55*10=550, 550/100=5.5
  final = price + tax # 55+5.5=60.5 
  return round(int(final)) # =60
   
   
print(color(inp)) # 60
You see, the result is correct.


RE: Problem with my code - ichabod801 - Dec-30-2019

The only think I can think of is that the tool is expecting a floating point number that includes pennies. Maybe change line 9 to return round(final, 2)?


RE: Problem with my code - Farcrafting - Dec-31-2019

Hello all and thanks for your answers.

I have solved the problem now.
it was because of a rounding error in the round() function...

This works now:) :

 inp = int(input())


def color(stk):
  colorprice = stk * 5
  price = colorprice + 40
  tax = (price * 10) / 100
  final = price + tax
  roun_d = lambda x : int(x+.5) if x>0 else int(x-.5)
  return roun_d(final)
  
 print(color(inp))
  
Wish you all a happy new year!!