Python Forum

Full Version: assert
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello! i have a problem assertions can you help me please?
in this code assert statement is always raised as true even if the condition is not me
x = ["Nr", "Mi", "Ki"]
y = ["Ha" ,"Se", "La"]
print("ill guess your name ;)")
gender = input("please enters if you are a Male or a Female").lower()
first_letter = input('put the first letter of your name').lower()
last_letter = input('put the last letter of your name').lower()
assert gender is int() or float(), "so your gender is a number okay ^^"
assert first_letter or last_letter is int() or float(), "there are no names with numbers in this world isn't it?!"
if gender == "male" and first_letter == "n" and last_letter == "r":
    print(x[0])
elif gender == "male" and first_letter == "m" and last_letter == "i":
    print(x[1])
elif gender == "male" and first_letter == "k" and last_letter == "d":
    print(x[2])
elif gender == "female" and first_letter == "h" and last_letter == "a":
    print(y[0])
elif gender == "female" and first_letter == "s" and last_letter == "e":
    print(y[1])
elif gender == "female" and first_letter == "l" and last_letter == "r":
    print(y[2])
output:
Output:
ill guess your names ;) please enters if you are a Male or a Female male put the first letter of your name m put the last letter of your name i Traceback (most recent call last): File "D:/Python/Medi/Medi.py", line 7, in <module> assert gender is int() or float(), "so your gender is a number okay ^^" AssertionError: so your gender is a number okay ^^
thank you!
This is not valid verification of the number type:
gender is int() or float()
And the hidden problem is that you get your variables as a strings (with the input() function). So the similar valid verification wouldn't succeed anyway.

You can write down and use some function for a number like strings validation:
def is_string_number(string: str) -> bool:
    # It validates integers as well as floats
    try:
        # It tries to cast the string to the float
        float(string)
        # It it succeed it returns True
        return True
    except ValueError:
        # If it failed it returns False
        return False