Help in tweaking a code

i have this below i have watched in and practiced a tutorial, however i tired to achieve the same outcome in a different logic and it has not been fairly correct. i need help with the code.
Code one allows me to get feed back if certain conditions are met, i tired the same method with code two but its not giving back the right results
Code 1
num = 32
tries = 3
guess = input("guess a number btn 1 and 50: ")
guess = int(guess)

while guess != num and tries != 0:
guess = input("guess a number btn 1 and 50: ")
guess = int(guess)
if guess == num:
print(“Congratulations”)
elif guess != num and tries == 1:
print(“Better luck next time”)
break
elif guess > num:
tries -= 1
print(“Guessed too high”)
else:
tries -= 1
print(“Guessed too low”)

please ask for further clarifications if its not explain well:
Code 2

def get_age():
age = ‘’
tries = 3

while age.isdigit() == False and tries != 0:
    age = input("Your age is? ")

    if age.isdigit() == True:
        print(age)
    
    elif age.isdigit() == False and tries == 1:
        print("Better luck next time")
        break
    elif age.isdigit() == False:
        tries -= 1
return int(age)

get_age()

other ways i tried in images:

You’re not using the .isdigit() method correctly.

As an example:

age = input("Your age? ")

if age.isdigit():  # this will return True if all the characters are digits, otherwise False
    print(f"Your age is: {age}")
else:
    print("That was not a integer number")

You’re also not formatting your posts correctly.

  1. Don’t post images please, unless you absolutely need to.
  2. Fence your code block (pre-post) like this:
```python
your code here
```
1 Like

thank you Rob will look into that of the isdigit(), i have to read further about good ways about posting or help.

No worries.

Here’s a few links that explain:

thank you so much Rob, am grateful to you.

1 Like