Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
assert
#1
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!
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assert failure jtcostel 1 1,638 Sep-03-2021, 05:28 PM
Last Post: buran
  Assert that dictionary contains a key phpMick 3 38,447 Nov-27-2019, 11:36 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020