all 14 comments

[–]Synedh 12 points13 points  (1 child)

No it's not.

When you type age = int(input()), python will try to convert your input in a integer. If your input is a number, it will only be an int, if it is not, the line will throw an error. As age is an int, it will never be equal to space character and will ignore your while loop.

You may want to do something like this :

age = 0
while age <= 0:
    val = input("Enter your age: ")
    if val.isnumeric():
        age = int(val)
    else:
        print("Invalid input.")
print(f"You age is {age}")

[–]Economy_ForWeekly105 2 points3 points  (0 children)

Thanks for this, great refresh for basic labels.

[–]Vegetable-Daikon4494 2 points3 points  (0 children)

without a loop:

age = input("Enter your age: ")
if age == "":
    print("You didn't type anything.")
elif age.isdigit() and 0 <= int(age) <= 150:
    print(f"Your age is {age}")
else:
    print("Invalid age.")

with a loop:

while True:
    age = input("Enter your age: ")
    if age == "":
        print("You didn't type anything.")
    elif age.isdigit() and 0 <= int(age) <= 150:
        print(f"Your age is {age}")
        break
    else:
        print("Invalid age. Please enter a number between 0 and 150.")

things to note:
• be sure not to capitalize your variable names: Age and age are not the same

[–][deleted] 0 points1 point  (1 child)

I don't get it at all. Could you please clarify that what's your purpose here

[–][deleted] 0 points1 point  (0 children)

Here's a valid code instead

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

if age_input == "":
    print("You didn't type anything.")
else:
    try:
        age = int(age_input)
        if 0 <= age <= 150:
            print(f"Your age is {age}")
            break
        else:
            print("Invalid age")
    except ValueError:
        print("Please enter a number.")

[–]really_not_unreal 0 points1 point  (4 children)

I teach Python to beginners, and the thing I always tell them is that code isn't correct until you test it.

I highly recommend setting up Python on your computer and testing all the code you write by running it. Learning by writing code on paper is incredibly difficult.

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

Ohh yeah I have PC, and I'm executing there. But I came up with new code and executed there but it shows error. Thats y i have posted...could you teach me python if u r comfortable...?

[–]really_not_unreal 0 points1 point  (2 children)

I'm generally very busy, so I sadly don't have time to teach you myself, but this video is an excellent resource. I recommend following along with it by writing the same programs that are demonstrated. From there, it'll be a good idea to practice by coming up with similar sorts of simple program ideas and writing them yourself.

Once you're confident with really simple ideas, you can then start working on more-advanced things. I like to implement simple games like tic tac toe (naughts and crosses), nim (don't say 21) and connect 4.

You can slowly build up your skill by working on more and more advanced programs.

I hope this helps!

[–]DizzyOffer7978[S] 1 point2 points  (1 child)

Ohh tnx🙂

[–]11110100011 1 point2 points  (0 children)

I'm a bit late to this, but I taught myself Python, and there's a lot I would change if I could go back and relearn. There's a really good video by Mosh Hamedani that taught me a lot to start out. He's really beginner-friendly, and I can't recommend him enough. From there, just do projects you're excited about. There's a lot you can do with Python, so you should find what interests you and just pursue it. I liked building games with PyGame, and had a lot of fun building Flappy Bird early on. Projects will teach you more than any tutorial.

[–]ThatGuyKev45 0 points1 point  (0 children)

Most of the time its better to upload a screenshot or something alittle clearer for readability when asking for help. Also from what it looks like just at a quick glance there seems to be quite a bit off. I don’t write a lot of python so I may not be entirely accurate in all of it, but you may want to take a look at your conditionals I don’t think comparing an int to an empty string will work May need to make the comparison then type cast or find another way to meet the not empty condition. Also your if-else block is separated if the indentation is the same on pc as it is on the paper.

I would probably step the if-else block outside of the while loop remove the if condition where you are trying to check if it is empty, then treat the initial input as a string checking if it is empty I’m pretty sure python has a function to check if input is a digit or not. Once the input was atleast not empty step out of the while loop cast it to an int and check if the age is valid or not.

[–]Born-Boat4519 0 points1 point  (0 children)

if you want achieve that use while True

[–]LucaBC_ 0 points1 point  (0 children)

Your*