0

Just cant seem to figure this out..... python

"Write a program which repeatedly reads numbers until the user enters “done”. Once “done” is entered, print out the total, count" I understand the question and can write it out in English but no matter what I try I can't get it to do it. I have started all over so many times now. the chapter ( self-taught) does not give enough info to do this. please help... dont mind the "#" that is just some different attempts n = 0 while True: user = input('Enter Numbers:') #if n != 0: #n = n + float(user) if user == 'done': user = n n = n + float(user) total = sum(n) print(total) print('Total:', total) break

17th Oct 2017, 7:15 PM
Aric Dunn
Aric Dunn - avatar
16 Respuestas
+ 3
n=0 user = input("enter numbers") while user != "done": n+=float(user) user = input("enter numbers") print(n)
17th Oct 2017, 7:56 PM
Oma Falk
Oma Falk - avatar
+ 1
Thank you that helps a little. I am still confused...
17th Oct 2017, 8:02 PM
Aric Dunn
Aric Dunn - avatar
+ 1
The logic of the code I guess. having trouble making sense of what is actually happening.. I'm a math person ( a physicist) and the logic is killing me I am just trial and error where to put stuff lol.. this is the full question and where I am at now... I got it to work so far .. but the try except is spitting out a infinite loop now.. a "continue" and "break" probably needs to go somewhere... I think "Write a program which repeatedly reads numbers until the user enters “done”. Once “done” is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number." n = 0 count = 0 user = input('Enter Numbers:\n> ') while user != 'done': try: count = count + 1 n = n + float(user) user = input('> ') avg = n/count if user == 'done': print('Total',n) print('Count:',count) print('Average:',avg) except: print('Please Enter or "done"')
17th Oct 2017, 9:04 PM
Aric Dunn
Aric Dunn - avatar
+ 1
Your logic is correct and your program works like you said but in Code Playground SoloLearn is different because only it only will show you 1 input so if you write for example with new línea (enter) 5 6 9 done you must to see a result, finishing inifinity while. Total: 30 Count: 3 Average: 10
17th Oct 2017, 9:15 PM
Daniel
Daniel - avatar
+ 1
Sorry I am not sure what you mean? I am not using the sololearn playground. Just trying to get the try/except to work now. and my logic is just guessing and trial and error not even sure what I did lol...
17th Oct 2017, 9:26 PM
Aric Dunn
Aric Dunn - avatar
+ 1
Ahh ok, I think I understand you now. Well you can check that http://www.pythonforbeginners.com/error-handling/exception-handling-in-JUMP_LINK__&&__python__&&__JUMP_LINK to try to understand except better
17th Oct 2017, 9:32 PM
Daniel
Daniel - avatar
+ 1
right.. I have used the try/except before but the question is asking me to skip to the next number which I cant get to work.. just keeps making an infinite loops for some reason... "If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number."
17th Oct 2017, 9:43 PM
Aric Dunn
Aric Dunn - avatar
+ 1
ok............. now I am at a loss... I got the try/except in the right spot so it at least skips to next number... however now the count is wrong. the average is including the string Code n = 0 count = 0 user = input('Enter Numbers:\n> ') while user != 'done': count = count + 1 try: n = n + float(user) except: print('Enter Numbers or "done"') user = input('> ') avg = n/count if user == 'done': print('Total',n) print('Count:',count) print('Average:',avg) output Enter Numbers: > 4 > 5 > bad Enter Numbers or "done" > 7 > done Total 16.0 Count: 4 Average: 4.0 Process finished with exit code 0 the average is doing ( 4 + 5 + "bad" + 7)/4 when it should not include bad...
17th Oct 2017, 9:55 PM
Aric Dunn
Aric Dunn - avatar
+ 1
Maybe is better to use if statement only to run each instruction per each case
17th Oct 2017, 10:54 PM
Daniel
Daniel - avatar
+ 1
im not sure what you mean.. im just a beginner
17th Oct 2017, 11:01 PM
Aric Dunn
Aric Dunn - avatar
+ 1
Change try and except only for If, else if, else
17th Oct 2017, 11:26 PM
Daniel
Daniel - avatar
+ 1
oh ok. ill try.
17th Oct 2017, 11:27 PM
Aric Dunn
Aric Dunn - avatar
+ 1
Share your code from your playground if you need help
17th Oct 2017, 11:31 PM
Daniel
Daniel - avatar
+ 1
yeah... only place it works is around the n variable but then the average is wrong becasue of the count.. I just tried the try/except around the if statement and it failed. n = 0 count = 0 user = input('Enter Numbers:\n> ') while user != 'done': try: n = n + float(user) except: print('Enter Numbers or "done"') user = input('> ') count = count + 1 # if user != 'done' or user != n: #count = count avg = n/count if user == 'done': print('Total:',n) print('Count:',count) print('Average:',avg)
17th Oct 2017, 11:33 PM
Aric Dunn
Aric Dunn - avatar
0
Why confused yet?
17th Oct 2017, 8:55 PM
Daniel
Daniel - avatar
0
You can try using repl.it to run python programs
15th Nov 2020, 12:39 AM
Oswaldo Rodriguez
Oswaldo Rodriguez - avatar