Reputation: 69
Basic task of making code that can find the sum & average of 10 numbers from the user.
My current situation so far:
Sum = 0
print("Please Enter 10 Numbers\n")
for i in range (1,11):
num = int(input("Number %d =" %i))
sum = Sum + num
avg = Sum / 10
However, I want to make it so that if the user inputs an answer such as "Number 2 = 1sd" it doesn't stop the code immediately. Instead I'm looking for it to respond as "Invalid input. Try again" and then it restarts from the failed input.
e.g.
"Number 2 = 1sd"
"Invalid input. Try again."
"Number 2 = ..."
How would I achieve that?
Upvotes: 3
Views: 263
Reputation: 154
If your data is not too much, I think put them in a list is better. Because you can easier change program to calculate others if you want.
nums = [] # a list only store valid numbers
while len(nums) < 10:
input_n = input("Number %d = " % (len(nums) + 1))
if input_n.isdigit():
n = int(input_n)
nums.append(n)
else:
print("Invalid input. Try again.")
# average is more commonly known as a float type. so I use %f.
print("Average = %f" % (sum(nums) / len(nums)))
Upvotes: 0
Reputation: 1
from statistics import mean
i=0
number_list = []
while i <= 9:
num = input("Please Enter 10 Numbers\n")
if num.isdigit() ==False:
print('Invalid input. Try again.')
num
else:
number_list.append(int(num))
i +=1
print(sum(number_list))
print(mean(number_list))
Upvotes: 0
Reputation: 998
This one is working for me.
sum_result = 0
print("Please Enter 10 Numbers\n")
for i in range (1,11):
try:
num = int(input("Number %d =" %i))
sum_result = sum_result + num
except Exception:
print("Please try again")
num = int(input("Number %d =" %i))
sum_result = sum_result + num
avg_result = sum_result / 10
print(sum_result)
print(avg_result)
Suggestions:
Upvotes: 2
Reputation: 1249
You can output an error message and prompt for re-entry of the i
th number by prompting in a loop which outputs an error message and reprompts on user input error. This can be done by handling the ValueError
exception that is raised when the input to the int()
function is an invalid integer object string initializer such as as "1sd"
. That can be done by catching that exception, outputting Invalid input. Try again.
and continuing when that occurs.
One possible resulting code that may satisfy your requirements is:
Sum = 0
print("Please Enter 10 Numbers\n")
for i in range (1,11):
num = None
while num is None:
try:
num = int(input("Number %d =" %i))
except ValueError:
print('Invalid input. Try again.')
sum = Sum + num
avg = Sum / 10
Upvotes: 3
Reputation: 1214
you can check the input, then continue or return an error, something like this:
sum = 0
print('Please Enter 100 Numbers:\n')
for i in range(1, 11):
num = input('Number %d' % i)
while not num.isdigit():
print('Invalid input. Try again.')
num = input('Number %d' % i)
sum += int(num)
avg = sum / 10
print('Average is %d' % avg)
Upvotes: 1
Reputation: 203
You can do:
while True:
try:
num = int(input('Enter an integer: '))
break
except Exception:
print('Invalid input')
#now num is an integer and we can proceed
Upvotes: 1