Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
You go to a stationary shop where pens are sold in a specific manner. The first pen costs Rs. x, second one costs Rs. 2x, and nth pen costs Rs. nx. If you want y pens, and have only Rs. w with which you went to the shop with, then you have to buy some on credit (owing the shopkeeper). Write a program to compute how much you will owe based on the input provided. The input will contain 3 integers, x (cost of the first pen), then w (amount of money you went to shop with), then n (number of pens that you need). Output the amount of money you will owe the shopkeeper.

What I have tried:

x = int(input())
w = int(input())
n = int(input())

credit = n*(n+1)*x/2 - w
print (credit)

This code is not functioning on mimir
Posted
Updated 19-Apr-22 1:39am
Comments
CPallini 19-Apr-22 7:06am    
The program looks correct.
Whats' the trouble with Mimir?
(note you may use integer division (//), because n*(n+1) is always even.
study lover 19-Apr-22 7:12am    
One of the test case is negative why?
Patrice T 19-Apr-22 7:43am    
And you plan to tell the inputs of that test case ?

Quote:
One of the test case is negative why?

At a guess - and since we can't see the test case data at all that's all it can be - n*(n+1)*x/2 is less than w

Think about it: each time you go shopping and pay in cash, do you ever owe the shopkeeper a negative amount of money? Or do you owe him nothing if you pay X and have X + 10 in your pocket?
 
Share this answer
 
Comments
study lover 19-Apr-22 7:49am    
so how to solve this problem
OriginalGriff 19-Apr-22 8:16am    
How do *you* think?

This isn't complicated - just think about what you would do in a real world situation.

It's beginning to look like you are trying to learn this by guesswork, instead of following a structured course or book ... and if so, you won't ever get anywhere useful ...
Your time would be better spent actually learning Python from the beginning, rather than posting here: The Python Tutorial — Python 3.9.12 documentation[^]
 
Share this answer
 
Comments
study lover 19-Apr-22 7:51am    
I am trying questions to learn from my mistakes and I have learned few basics and was unable to identify my mistakes
Richard MacCutchan 19-Apr-22 8:05am    
Which is exactly why I suggested you study the tutorial, as that will teach you in a structured manner. Trying to learn by posting questions here every time your code does not work will take you forever.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900