0

I'm learning python at this moment and i was wondering how i can make python ask the user to fill in a other number if the user didn't fill in a number but a word. Below is my code how i have it now my guess is that i have to put it somewhere in the Except part, but i don't know what. Thanks in advance

numlist = list()

try:
    while True :
        inp = input('Enter a number ')
        if inp == 'Done' : break
        value = float(inp)
        numlist.append(value)

    Average  = sum(numlist) / len(numlist)
except:
    print ('This is not a number, please enter a number')


print ('The average is: ', Average)
Idrowzzyy
  • 19
  • 8
  • So you want to continuously ask the user to input a valid number when `float(inp)` fails, right? What construct does one use to repeat a piece of code? You're almost there, just think about where to place the `try/except` block. – ForceBru May 05 '20 at 12:19
  • What have you tried so far and what did you expect to happen and what was different? Please ask a precise question. – LinFelix May 05 '20 at 12:19
  • Just swap try/except and while blocks. ie. use try/except within while. As simple as that :) – Faizan Naseer May 05 '20 at 12:22
  • Thank you for the answers, i think i can manage now. Have to go to work now, will post if i succeeded or not after work – Idrowzzyy May 05 '20 at 12:29

0 Answers0