Python icon indicating copy to clipboard operation
Python copied to clipboard

Need help for my age controller

Open d1ngil opened this issue 8 months ago • 16 comments

Feature description

I want to make something for if someone’s gonna write a str instead of int in this input it needs to write “age is not valid” I’m a starter i can’t did it im confused

Image

d1ngil avatar Jun 26 '25 11:06 d1ngil

Hi @d1ngil, I'd like to work on this issue if it's still open. I can add proper input validation for age, handling both string and negative values.

Let me know if you have any specific constraints or if I can go ahead with a PR.

Hello of course you can work on it, it's open but i don't know what to do im new in github

d1ngil avatar Jun 27 '25 20:06 d1ngil

try:
    age = int(input("Enter your age: "))
    if age < 0:
        raise ValueError("Age cannot be negative.")
    else:
        print(f"Your age is: {age}")
except ValueError as e:
    print("Invalid input, Enter age in Integer")

Here's your code you can use try, except block for printing invalid input.

itsmoksh avatar Jun 29 '25 07:06 itsmoksh

try:
    age = int(input("Enter your age: "))
    if age < 0:
        raise ValueError("Age cannot be negative.")
    else:
        print(f"Your age is: {age}")
except ValueError as e:
    print("Invalid input, Enter age in Integer")

Here's your code you can use try, except block for printing invalid input.

@MoksH-Jain05 we have to maintain his condition that is the while loop, we continuous taking the value. Maybe that was a part of his whole code.

30November avatar Jun 30 '25 08:06 30November

while True:
    age = input("Enter your age:")
    if age.isdigit( ):
        num = int(age)
        if num < 0: print("Age can't be negative")
        else: break 
    else: print("Invalid age")

print(f"Your age is {age}")

Here is the true define code, anymore doubt please feel free to ask. After full clarification and finally gets clear and done, I request you to remove from that issue section.

THANK YOU

30November avatar Jun 30 '25 08:06 30November

while True:
    age = input("Enter your age:")
    if age.isdigit( ):
        num = int(age)
        if num < 0: print("Age can't be negative")
        else: break 
    else: print("Invalid age")

print(f"Your age is {age}")

Here is the true define code, anymore doubt please feel free to ask. After full clarification and finally gets clear and done, I request you to remove from that issue section.

THANK YOU

Bro i know that but he asked for only data validation that's why i have not used while loop

itsmoksh avatar Jun 30 '25 08:06 itsmoksh

loop

Ohh okay, make sense hehe

30November avatar Jun 30 '25 09:06 30November

try: age = int(input("Enter your Age :")) if age > 0: print(f" your Age is {age}") elif age <= 0: print("Age should be greater than 0")

except Exception as e: print("Age is not Valid") print(e)

Here is the Basic code to overcome from the issue.

abhishekmvgowda avatar Jul 13 '25 21:07 abhishekmvgowda

while True:
    age = input("Enter your age: ")

    try:
        num = int(age)
        if num < 0:
            print("Age can't be negative")
        else:
            break
    except ValueError:
        print("Age is not valid")

print(f"Your age is {num}")

This is the correct code you need — handles invalid input and negative ages properly.

Amartya-007 avatar Jul 16 '25 11:07 Amartya-007

try the below code while True: age_input = input("Enter your age: ")

try:
    age = int(age_input)  # Try converting input to a number
    if age < 0:
        print("Age can't be negative.")
    else:
        break  # Valid age, exit loop
except ValueError:
    print("Age is not valid. Please enter a number.")

print(f"Your age is {age}")

adityakaldate21-dev avatar Jul 29 '25 11:07 adityakaldate21-dev

while True: try: age = int(input("Enter your age: ")) if age < 0: print("\nAge can't be negative") else: break
except ValueError: print("Age is not valid")

print(f"\nYou are {age} years old") I hope this helps , you just need to use the try- except block for the proper output

shrutim250 avatar Aug 04 '25 13:08 shrutim250

**The person’s problem is that if someone types a string (like "abc") instead of a number for age, the program crashes instead of saying "age is not valid".

You can solve this using a try-except block to handle invalid inputs:**

while True:
    try:
        age = int(input("Enter your age: "))
        
        if age < 0:
            print("Age can't be negative")
        else:
            print(f"You are {age} years old")
            break  # exit loop if valid age
    except ValueError:
        print("Age is not valid")

**How this works:
try block → tries to convert the input to an integer.

except ValueError → catches the case when the input is not a number and prints "Age is not valid".

If the age is negative, it warns the user.

If the age is valid, it prints the result and breaks out of the loop.**

DeveshPatill avatar Aug 12 '25 07:08 DeveshPatill

🧾 Description

This function checks if the input age is valid (between 0 and 110).
If the input is invalid (not an integer or out of range), the program prints an error message.

🧩 Code Example

# The age that i have taken (0-110) is a estimate for a proper human age things can vary  thanks !
def age_checker(age):
    if age < 0 or age > 110:
        raise ValueError("Enter a valid possible age!")
    return age

if __name__ == "__main__":
    try:
        age = int(input("Enter your age: "))
        result = age_checker(age)
        print(f"You are {result} years old")
    except ValueError:
        print("age is not valid")

AtharSayed avatar Aug 13 '25 12:08 AtharSayed

Hi, can I work on this issue ?

kadubhumika avatar Sep 17 '25 04:09 kadubhumika

def get_age_input(): age_input = input("Enter your age: ") if age_input.isdigit(): age = int(age_input) print(f"Your age is {age}") else: print("age is not valid")

get_age_input()

What this does:

  1. Takes input from the user.

  2. Checks if the input only contains digits using .isdigit().

  3. If it is digits, it converts it to an int and prints it.

  4. If it’s not (e.g., letters, special characters), it prints "age is not valid"

Shweyy123 avatar Oct 01 '25 06:10 Shweyy123

def get_age_input(): age_input = input("Enter your age: ") if age_input.isdigit(): age = int(age_input) print(f"Your age is {age}") else: print("age is not valid")

get_age_input()

What this does:

Takes input from the user.

Checks if the input only contains digits using .isdigit().

If it is digits, it converts it to an int and prints it.

If it’s not (e.g., letters, special characters), it prints "age is not valid"

fzxcntrl avatar Oct 01 '25 20:10 fzxcntrl

while True: try: # Prompt the user for input age_input = input("Enter your age: ")

    # Try to convert the input to an integer
    age = int(age_input)

    # Check for negative age
    if age < 0:
        print("\nAge cannot be negative. Please try again.")
        continue # Go back to the start of the loop
    
    # If both checks pass, exit the loop
    break

except ValueError:
    # This block executes if int(age_input) fails (e.g., if user enters 'abc')
    print("\nAge is not valid. Please enter a whole number.")

Print the final result once a valid age is obtained

print(f"\nYou are {age} years old")

Asukasuk avatar Dec 14 '25 11:12 Asukasuk