+ 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
21 ответ
+ 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
+ 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.
+ 2
Lisa so I cant use list right now....
+ 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!
+ 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 :)
+ 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
+ 1
Lisa thank you so muchh i got it now
+ 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)
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.
0
Lisa acctually i am just knowing (if, else, elif, while)
0
Lisa is there any other way than append?
0
Lisa basic way which i can do?
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?
0
# in loop
if mini%2 != 0:
sum = sum + mini
n = n + 1
# in the end
print(sum)
print(sum/n)
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....
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)
0
harmeet singh I added comments in my code example
0
sum should be called outside while loop function
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