简体   繁体   中英

Why wont the while loop stop?

I think im missing something here to make the while loop end because everytime I try to calculate the average for everything it asks me if I have another client to enter. Also, what am I missing to get the average for everyone? The value I get is never right.

csum=0
ccount=0
clients=int(input("How Many Clients?"))
while clients==clients:
    sum=0
    counter=0
    determiner=str(input("Do you have another client to enter?"))
    if determiner==str('Yes' or "Y" or "YES"):
        clientname=str(input("What is the client's name?"))
        for i in range(4):
            stock=int(input("Enter Stock Amount:"))
            sum=sum+stock
            counter=counter+1
            avg=sum/counter
            csum=csum+avg
        print("The Average for",clientname,"is:",avg)
    if determiner==str('No' or 'no' or 'NO'):
        cavg=csum/clients
        print("The average for all the clients is:",f'{cavg:.2f}')

And output is:

How Many Clients? 3
Do you have another client to enter?Yes
What is the client's name?Owen
Enter Stock Amount:5000
Enter Stock Amount:-6000
Enter Stock Amount:7000
Enter Stock Amount:3000
The Average for Owen is: 2250.0
Do you have another client to enter?Yes
What is the client's name?John
Enter Stock Amount:1000
Enter Stock Amount:2000
Enter Stock Amount:3000
Enter Stock Amount:4000
The Average for John is: 2500.0
Do you have another client to enter?Yes
What is the client's name?Matt
Enter Stock Amount:7000
Enter Stock Amount:8000
Enter Stock Amount:6000
Enter Stock Amount:5600
The Average for Matt is: 6650.0
Do you have another client to enter? No
The average for all the clients is: 14633.33
Do you have another client to enter?

Some suggestions for your code:

  • Change while clients==clients to while True
  • Remove str conversions. Inputs are str by default.
  • Change if conditions to:
    if determiner.lower() in {"yes", "y"}: ...
    if determiner.lower() in {"no", "n"}: ...
  • You can use less amount of variables to achieve the same result

  • The answer to your problem according to this code is the missing break :

    if determiner.lower() in {"no", "n"}:
        cavg = csum/clients
        print(f"The average for all the clients is: {cavg:.2f}")
        break

The main problem is with missing break statement here

if determiner==str('No' or 'no' or 'NO'):
    cavg=csum/clients
    print("The average for all the clients is:",f'{cavg:.2f}')
    break

Also, if you have had already asked for number of clients int(input("How Many Clients?")) why not just use this to create a for loop and you would not even need to ask if there is another client:

clients=int(input("How Many Clients?"))
csum = 0
number_of_stocks = 4
for i in range(clients):
    client_sum=0
    clientname=str(input(f"What is the client #{i+1}'s name?"))
    for i in range(number_of_stocks):
        stock=int(input("Enter Stock Amount:"))
        client_sum+=stock
    avg=client_sum/number_of_stocks 
    csum=csum+avg
    print("The Average for",clientname,"is:",avg)

cavg = csum/clients
print("The average for all the clients is:",f'{cavg:.2f}')

Furthermore I have had changed sum to client_sum as it is a keyword in Python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM