Reputation: 19
"For this task you have the chance to show what you’ve learned about implementing repetition in code. You are required to create a program that uses the while loop structure.
Write a program that always asks the user to enter a number. When the user enters the negative number -1, the program should stop requesting the user to enter a number. The program must then calculate the average of the numbers entered excluding the -1.
Make use of the while loop repetition structure to implement the program."
I got this far and am not sure how to list the input numbers on an infinite range??
#Ask the user to pick a number
print("Pick a number")
attempt = 1
answer =
int(input("Answer: ")), [],0
#While the answer does not equals -1 pick another number
while answer != -1:
print("Pick another number.")
# increase the attempt number
attempt += 1
print("Attempt number " + (str(attempt)))
# increase the answer total
answer=[]
for n in range(attempt):
answer.append(answer)
print("Answer total " + (str(answer)))
# ask the user again for an input
answer = int(input("Answer: "))
if answer==-1:
print ("The total averge of your answers is" + sum(answer))
Upvotes: 0
Views: 1000
Reputation: 1
the problem is that you are considering the situation wrongly by restating the answer array to an empty one at each iteration. What you need to do is that you keep track of the answers with that array, because you will need them afterward to calculate the average. Moreover, make sure to save at each iteration the value entered by the user, so that you can compare it with -1 (in your code, you are comparing the array to -1 which is absurd).
take a look at this code, it may help.
#Ask the user to pick a number
print("Pick a number")
attempt = 1
answer = []
# a variable to keep track of the values that the user give
currentValue = int(input("Answer: "))
# append this variable to the array of the answers
answer.append(currentValue)
#While the answer does not equals -1 pick another number
while currentValue != -1:
print("Pick another number.")
# increase the attempt number
attempt += 1
print("Attempt number " + (str(attempt)))
# ask the user again for an input
currentValue = int(input("Answer: "))
# if the user wants to quit, output the result
if currentValue ==-1:
print ("The total averge of your answers is" , sum(answer) /
len(answer))
# else, just append the value to our array of values
else:
answer.append(currentValue)
Upvotes: 0
Reputation: 1432
The basic things you want to keep track of are: the running sum (an integer) and the total number of attempts (also an integer). I have reordered your code to track these two, and then when the user inputs -1 we calculate the average by dividing the running sum by the number of attempts.
#Ask the user to pick a number
print("Pick a number")
num_attempt = 0
answer = 0
answer_sum = 0
# While the answer does not equals -1 pick another number
while answer != -1:
# ask the user again for an input
answer = int(input("Answer: "))
if answer == -1:
print ("The total average of your answers is " + str(answer_sum / num_attempt))
break
# increase the attempt number
num_attempt += 1
print("Attempt number " + (str(num_attempt)))
# increase the answer total
answer_sum += answer
print("Answer total " + (str(answer_sum)))
print("Pick another number.")
Upvotes: 0