python - Using a break statement in a loop -


write program uses while loop repeatedly prompt user numbers , adds numbers running total. when blank line entered, program should print average of numbers entered. you need use break statement exit while loop. here have gotten far:

number_of_entries = int(input('please enter number of numbers: ')) entries = [] count = 0 while count < number_of_entries:     entry = float(input('entry:'))     entries.append(entry)     count = count + 1 average = sum(entries)/number_of_entries print(average) 

this first prompts user enter number of numbers prompts them entries while loop continues prompting numbers until count of numbers equals number of numbers user inputted. entries saved list , sum of numbers in list used average stored in average variable. if user doesn't input (just press enter) supposed find average of numbers inputted rather result in error code. know have use break statement can't figure out.

break if user presses enter or cast , append

while true:     entry = input('entry:')     if not entry: # catch empty string , break loop         break     entries.append(float(entry))   average = sum(entries)/len(entries) 

if breaking when user hits enter number_of_entries irrelevant. can use length of list average, if user entered 10 number_of_entries original while count < number_of_entries: , hit enter after inputting 5 numbers average = sum(entries)/number_of_entries wrong.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -