Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 assert Naito Silly Frenchman Posts: 25 Threads: 11 Joined: Jan 2019 Reputation: 1 Likes received: 2 #1 Jan-16-2019, 10:52 AM (This post was last modified: Jan-16-2019, 10:53 AM by Naito. Edited 2 times in total.) 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 assert gender is int() or float(), "so your gender is a number okay ^^" AssertionError: so your gender is a number okay ^^``````thank you! ODIS Silly Frenchman Posts: 46 Threads: 0 Joined: Nov 2017 Reputation: 2 Likes received: 11 #2 Jan-16-2019, 05:06 PM (This post was last modified: Jan-16-2019, 05:06 PM by ODIS. Edited 6 times in total.) 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 ``` Naito likes this post « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)