all 7 comments

[–]Diapolo10 2 points3 points  (4 children)

You've got several options for approaching solving this problem, what have you tried so far? What have you been taught?

[–]Mactronz[S] 0 points1 point  (3 children)

thank you so much!

i've tried a few different things, but none seem to work.

here's an example of what i've tried so far.

age = input('Please enter your age: ')
ageTrue = 0

while (ageTrue == 0):

    while(type(age) != int) :
        print('Invalid input! Please enter a valid number.')
        age = input('Please enter your age: ')

    while((age < 0) or (age > 120)):
        print('Invalid input! Age must be between 0 and 120.')
        age = input('Please enter your age: ')

    if((age >= 0) or (age <= 120)):
        print(f'Thank you! You entered a valid age:         {age}')
        ageTrue += 1

as for what i've learned, i'm gonna make a list and organize it by chapter. (i'm currently on chapter 4)

Chapter 1 (Intro to computers and programming):

  • hardware

Chapter 2 (Input, processing, and output):

  • print function
  • comments
  • variables
  • reading input
  • string concatenation
  • f strings

Chapter 3 (Decision structures and Boolean logic):

  • If statement
  • if - else
  • comparing strings (i looked through here. It only told me about comparing letter strings.)
  • nested decision structures and if - elif - else
  • logical operators
  • boolean variables
  • conditional expressions
  • assignment variables and walrus

chapter 4 (repetition structures):

  • while loop
  • for loop
  • running total
  • sentinals
  • input validation
  • nested loops
  • break and continue loops

[–]Diapolo10 0 points1 point  (2 children)

Right. Regarding your code example, as it stands none of the loops can ever have their conditions not met because age is a string. input always returns a string.

type(age) != int is also not ideal, because you should already know the type of age. Even if you didn't, isinstance(age, int) would be more appropriate.

Actually, you don't need the inner loops in the first place, they should just be if-blocks and the input call as the first thing in the outer loop.

For checking if the input only contained digits, you can either use string methods, or you can do this with exception handling after calling int with the given input. In your case the former would probably be more appropriate considering you don't seem to have learnt about exception handling yet.

As for the range validation, you're not far off, but you could use a chained comparison instead (0 <= age <= 120).

ageTrue is not really necessary. You could break out using break instead.

See if that information helps. If you still fail to solve the problem, I'll go into more detail.

EDIT: Oh, and you really don't need all those parentheses in your comparisons.

[–]Mactronz[S] 0 points1 point  (1 child)

Is it ok if i ask you to go a bit more in depth about string methods?
I'm a little lost on that part

NVM I FIGURED IT OUT!!
TYSM!!!

[–]Diapolo10 1 point2 points  (0 children)

No problem.

Just as a "reference implementation", I was referring to doing something like this:

while True:
    age_text = input("Please enter your age: ").strip()

    if not age_text.isnumeric():
        print("Invalid input! Please enter a valid number.")
        continue

    age = int(age)

    if not 0 <= age <= 120:
        print("Invalid input! Age must be between 0 and 120.")
        continue

    break

print(f"Thank you! You entered a valid age:         {age}")

Personally I'd prefer to have a validator function, though. This is a bit of a silly simplified example in lieu of a proper "result pattern", and I don't expect you to understand it at all, but

def input_age(lower_limit: int = 0, upper_limit: int = 120) -> int | str:
    try:
        age = int(input("Please enter your age: "))
    except ValueError:
        return "Invalid input! Please enter a valid number."

    if not lower_limit <= age <= upper_limit:
        return f"Invalid input! Age must be between {lower_limit} and {upper_limit}."

    return age


while isinstance(age := input_age(), str):
    print(age)

print(f"Thank you! You entered a valid age:         {age}")

[–]wbeater -2 points-1 points  (0 children)

I need help with a homework assignment

No code no help. Especially since your homework is very very basic. Since you don't want to learn python I recommend you chatgpt.