+ 4

I want to create a python code which gives me the sum, avg of odd numbers between the two numbers the user entered. (While loop)

I want to create it with while loop and also display the odd numbers between those two numbers entered by user along with there sum and average The code i wrote just displays the list of odd numbers between those two digits. mini=int(input("Enter the minimum number:- ")) maxi=int(input("Enter the maximum number:- ")) while(mini<maxi+1): if(mini%2!=0): print(mini, end=' ') mini+=1 Can anyone please add the ability to get and display the sum and average of all the odd numbers between those two entered? Without using function

10th Aug 2021, 6:55 AM
Dark Knight
Dark Knight - avatar
21 Réponses
+ 6
The problem is, you have not assigned sum before using it (you typed s instrad of sum above) Print the total sum and the average only after the loop. https://code.sololearn.com/cyIzQA0kI122/?ref=app
10th Aug 2021, 7:39 AM
Lisa
Lisa - avatar
+ 2
Make an empty list before you enter the loop. Whenever a muber is odd, append it to this list. Then, after the loop, calculate sum and average with this list.
10th Aug 2021, 7:03 AM
Lisa
Lisa - avatar
+ 2
Lisa so I cant use list right now....
10th Aug 2021, 7:12 AM
Dark Knight
Dark Knight - avatar
+ 1
I think you can do this as a beginner! # create an empty list before entering the loop odd_nums = [] # when mini is odd (inside the loop), append it to this list odd_nums.append(mini) Then you have all numbers for your calculations in odd_nums!
10th Aug 2021, 7:08 AM
Lisa
Lisa - avatar
+ 1
Well, I sketched a list-based approach for you, so now you know. If you are a beginner, I recommend starting the Python for Beginners course: lists are covered there :)
10th Aug 2021, 7:13 AM
Lisa
Lisa - avatar
+ 1
you could assign sum = 0 n = 0 before entering the loop. When you find an odd number, you add it to sum and increase n by one. At the end of the loop you have the sum and get the average as sum/n
10th Aug 2021, 7:19 AM
Lisa
Lisa - avatar
+ 1
Lisa thank you so muchh i got it now
10th Aug 2021, 7:48 AM
Dark Knight
Dark Knight - avatar
+ 1
minimum = 10 print("min",minimum ) maximum = 15 sum = 0 print("top sum",sum) count = 0 while minimum < maximum + 1: if minimum % 2 == 0: sum = minimum + sum print("sum inside",sum) minimum += 1 count +=1 print("sum outside",sum/count)
10th Aug 2021, 10:27 PM
Omar
Omar - avatar
0
Lisa Acctually i am just a beginner, so if you could do that for me i would learn from it and keep it in my mind the next time for sure.
10th Aug 2021, 7:07 AM
Dark Knight
Dark Knight - avatar
0
Lisa acctually i am just knowing (if, else, elif, while)
10th Aug 2021, 7:09 AM
Dark Knight
Dark Knight - avatar
0
Lisa i have just started python 3 days ago and what i want to add in this code can be done by while and all but i am unable to thats why i thought to get help from solo learn and keep it in my.mind from the next time onwards
10th Aug 2021, 7:10 AM
Dark Knight
Dark Knight - avatar
0
Lisa is there any other way than append?
10th Aug 2021, 7:16 AM
Dark Knight
Dark Knight - avatar
0
Lisa basic way which i can do?
10th Aug 2021, 7:16 AM
Dark Knight
Dark Knight - avatar
0
Lisa thanks a lot for helping, but trust me its sounding really complex in my head. I know i sound like a noob but can u add this approach to my code so that then i will dry run it and analyse what u wanted me to do?
10th Aug 2021, 7:22 AM
Dark Knight
Dark Knight - avatar
0
# in loop if mini%2 != 0: sum = sum + mini n = n + 1 # in the end print(sum) print(sum/n)
10th Aug 2021, 7:24 AM
Lisa
Lisa - avatar
0
Lisa mini=int(input("Enter the minimum number:- ")) maxi=int(input("Enter the maximum number:- ")) s=0 n=0 while(mini<maxi+1): if(mini%2!=0): sum=s+mini n=n+1 print(mini, end=' ') print(sum) print(sum/n) mini+=1 Its acctually not working as desired, i am sure its my mistake though....
10th Aug 2021, 7:32 AM
Dark Knight
Dark Knight - avatar
0
Lisa by the way whenever you have time can you tell me what exactly did you do to make my code the work i was aiming onto. Like i wanna understand the logic of taking s=0 and everything u added in my code. (Thanks a lot for helping so much)
10th Aug 2021, 9:37 AM
Dark Knight
Dark Knight - avatar
0
harmeet singh I added comments in my code example
10th Aug 2021, 9:46 AM
Lisa
Lisa - avatar
0
sum should be called outside while loop function
10th Aug 2021, 9:26 PM
Omar
Omar - avatar
0
harmeet singh How about this? :- min = int(input()) max = int(input()) sum = len = 0 while min <= max: if min & 1: print(min) sum += min len += 1 min += 1 print(sum) print(sum / len) # Hope this helps
12th Aug 2021, 4:44 AM
Calvin Thomas
Calvin Thomas - avatar