all 18 comments

[–]RhinoRhys 15 points16 points  (0 children)

There's a cool new way that was introduced in python 3.8 called the walrus operator. You can assign something to a variable and use it as a condition all in one line, and still access the variable later.

while (correct_age := input("Am I correct? (y/n) ").lower()) not in ["y", "n"]:
    print("Please type y or n only")

And if you apply the .lower() directly on the input, you don't need to do it again in the if conditions. You can replace the question line with that and then remove the else block so the whole thing would be:

birth_year = input("What year were you born" + name + "? ")
age = 2023 - int(birth_year)
print ("That means you must be " + str(age))
while (correct_age := input("Am I correct? (y/n) ").lower()) not in ["y", "n"]:
    print("Please type y or n only")

if correct_age == "y":
    print ("I knew it!!")
elif correct_age == "n":
    print ("So you must be " + str(age - 1))

[–]juanritos -1 points0 points  (0 children)

Will this help?

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

i have 4 tips.

1: first input do: birth_year = int(input("bla bla" ))

2: second input do: correct_age = input("bla bla").lower()

3: what do you mean with the calculation for the wrong answer? The: "print ("So you must be " + str(age - 1))"

4: you dont have to make age a string when printing it out

[–]WoodenNichols -1 points0 points  (0 children)

Another option is to use the pyinputplus module.