Home > Net >  Program that asks me how many numbers i want, then lets me introduce those numbers, and then sums al
Program that asks me how many numbers i want, then lets me introduce those numbers, and then sums al

Time:05-20

I'm sure this question has been answered millions of times but i couldn't make it work so if you happen to find the answer i solemnly ask you to copy-paste it for me. Thanks.

Need python to ask me how many numbers i want (x), then ask me for a number (x) amount of times, then sum all those numbers and divide it by (x). Here's what i tried and i'm not proud of.

counter=(int(input('how many numbers?: ')))

number= int(input('input first number: '))
strike=(0)
while(counter!=strike):
    num=(int(input('next number: ')))
    strike =1
    if(counter==strike):
        num=(int(input('next number: ')))
    elif(counter==strike):
        print(sum(number num)/counter)

CodePudding user response:


first of all, you can delete parenthesis in if and while loops cause they are unnecessary. second, after you get the first number you should set the strike to 1 it should look like something like this:
counter = (int(input('how many numbers?: ')))

number = int(input('input first number: '))
strike = 1
num = 0
while counter != strike:
    num  = (int(input('next number: ')))
    strike  = 1
ans = (num   number) / counter
print(ans)
   

CodePudding user response:

You could try:

counter = int(input("How many numbers?\n>>>"))

num = 0
for i in range(0, counter):
num  = int(input(f"Enter number {i   1} please:\n>>>"))

result = num / counter
print(f"Result: {result}")

The f before the " in the string makes it an f-string which means you can use curly brackets, {}, to add a variable to your string easily. The for loop combined with the range function makes the code inside run for a specific amount of times.

  • Related