0

So basically I'm pretty much new to python. The last time I tried learning python was version 5 or 6 (I kinda forgot) and unfortunately stopped learning it because of other irl things to focus on. Now I'm back but things seemed to have changed a bit on Python 3.8, And I need help!

Currently I'm trying to play around with user input where user inputs name and age. The problem I'm facing right now is with the age code lines, Im trying to make user put only intergers and when they put anything other than integer than it should loop back to original line. Currently, Im trying to use this:

def inputnumber(message):
    while true:
        try:
            userinput = int(input(message))
        except ValueError:
            print("Please input integers")
            continue
        else:
            return userinput
            break

The problem is basically python is bypassing all those codes and not outputting the message. I suspect it's my outdated use of codes from back at version 5 or 6.

Any of you got any ideas? Thank you in advance!

SirLad
  • 11
  • 2
  • 1
    The only problem I see with this code is `true` instead of `True`. – chepner Jun 19 '20 at 15:57
  • 1
    Both `continue` and `break`, as written, are redundant; the loop will continue implicitly after the `try` statement completes, and `break` won't be reached because the `return` will immediately exit the function, terminating the loop as a side effect. – chepner Jun 19 '20 at 15:58
  • 1
    Also don't forget to call the function – Chris_Rands Jun 19 '20 at 15:58
  • Hi chepner and Chris_Rands. This is the OP, just wanna say thank you for your answers. It really helped me out! Turns out yes, it's the "true" statement that was wrong and also I forgot to call my function (my bad).... Anyways, thx again guys! – SirLad Jun 19 '20 at 16:56

0 Answers0