15,884,629 members
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
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 ?

## Solution 1

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?

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 ...

## Solution 2

Your time would be better spent actually learning Python from the beginning, rather than posting here: The Python Tutorial — Python 3.9.12 documentation[^]

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.