Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

python - total all different numbers on one function that is looping

I wanna add the total of all bill using this function that is looping depends of what amount to add

    calls = int(input("Input Call Duration (seconds) : "))

    if (calls <= 60):
        bill = 0

    if (calls > 60 and calls <= 120):
        bill = (calls-60)*0.5

    if (calls > 120):
        bill = (calls-60)*0.5+20

    bill = bill + 20
    print("Call Duration =", calls, "
Cost = ", bill)

This was the code im using it to loop

while n < int(number_virtuals):

calls = int(input("Input Call Duration (seconds) : "))

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 + bill})
print(dataList)

Output

*

How many virtuals are you requesting? 2
enter number of calls60
Call Duration = 60 
Cost =  20
enter number of calls120
Call Duration = 120 
Cost =  50.0
[{'number_virtuals': '2', 'Bills': 150.0}]
Process finished with exit code 0

I want to total all the collected bills to the print(dataList) from various users but it gets what is the last bill value and adds it up to the last also, but i can't

question from:https://stackoverflow.com/questions/65652190/total-all-different-numbers-on-one-function-that-is-looping

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I indented the code and ran it. It works fine. I also added a few comments to help you.

Two key things.

  1. You should initialize bill = 0 inside the loop.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...