I indented the code and ran it. It works fine. I also added a few comments to help you.
Two key things.
You should initialize bill = 0 inside the loop.
You are adding bill twice before you store it into the list. See the .append() line. Removed one bill. If you want to double the bill, then you can keep it as bill + bill.
Here's the updated code:
number_virtuals = 3 #set the loop to go 3 times
n = 0 #set counter to zero
dataList = [] #initialized the list
while n < int(number_virtuals):
calls = int(input("Input Call Duration (seconds) : "))
bill = 0 #added this line. Initialize bill each time
if (calls <= 60): # first 200 call free
bill = 0
if (calls > 60 and calls <= 120): # 200 free after 1 re/call
bill = (calls-60)*0.5
if (calls > 120): # first 200 free 200-500 1 re/call after 2 re/call.
bill = (calls-60)*0.5+20
bill = bill + 20 # add 100 Rs rental
print("Call Duration =", calls, "
Cost = ", bill)
n += 1
dataList.append({"number_virtuals": number_virtuals, "Bills": bill})
#you are storing bill + bill. It should be just bill
print(dataList)
The output of the above code is:
Note I gave 3 iterations. So the list has 3 elements.
Input Call Duration (seconds) : 40
Call Duration = 40
Cost = 20
Input Call Duration (seconds) : 70
Call Duration = 70
Cost = 25.0
Input Call Duration (seconds) : 150
Call Duration = 150
Cost = 85.0
[{'number_virtuals': 3, 'Bills': 20}, {'number_virtuals': 3, 'Bills': 25.0}, {'number_virtuals': 3, 'Bills': 85.0}]
For 40 minutes, it gives the correct bill as 20.
For 70 minutes, it gives the correct bill of (70-60) = 10 * 0.5 + 20 = 25
For 140 minutes, it gives the correct bill of (150 - 60) = 90 * 0.5 + 40 = 85
The comments in your code says first 200 calls free. However you are looking for calls <= 60. Do you want to make it 200? Is it 200 seconds? 200 calls? Don't know. If you have more details, please post them in the question section so this answer can be modified to meet the correct requirements.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…