+ 1
I have a new problem that has come up a few times. please help me with some coding
name_gender_type = ['Female','female','girl','Girl','gal','Gal','Male','male','boy','Boy','guy','Guy'] gender = input() if gender in ['male', 'Male','boy','Boy','guy','Guy']: print ("You are going to be a remarkable gentleman!") elif gender in ['female','Female','girl','Girl','gal','Gal']: print ("You will be a stupendous lovely lady!") else: print ("Are you sure about that? (Try entering male or female)") How do i make it where if they dont put in a valid input (male, female, etc.) the code ends and they have to run it again? i still want the code to move on if they input an accepted answer though. return gives syntax errors and i cant find anything on the lines of end, quit, terminate that actually exists. please help
8 Respuestas
+ 2
I used lower() to make the input all lowercase so I don't have to distinguish between 'Male' and 'male' for example. I shortened name_gender_type as a consequence. I put a while loop checking if the input is not in the list, and asking for input again if this is the case. And I used list splicing to check whether the input was in the first 3 or last 3 elements of name_gender_type.
I only see now that you wanted the execution to end (and not loop) if the input is incorrect, but that's an easy fix:
name_gender_type = ['female','girl','gal','male','boy','guy']
gender = input("What is your gender?\n").lower()
print(gender)
if gender in name_gender_type:
if gender in name_gender_type[-3:]:
print ("You are going to be a remarkable gentleman!")
else:
print ("You will be a stupendous lovely lady!")
else:
print ("Are you sure about that? (Try entering male or female)")
+ 1
name_gender_type = ['female','girl','gal','male','boy','guy']
gender = input("What is your gender?\n").lower()
print(gender)
while gender not in name_gender_type:
print ("Are you sure about that? (Try entering male or female)")
gender = input("What is your gender?\n").lower()
if gender in name_gender_type[-3:]:
print ("You are going to be a remarkable gentleman!")
elif gender in name_gender_type[:3]:
print ("You will be a stupendous lovely lady!")
+ 1
>>was the shortening of name_gender_type just for convenience?
It had useless data, so yeah.
>>i understand what the while loop does (checking if the input is in the list) but how?
"gender not in name_gender_type" checks all the elements of the list and try to find the value of gender, and returns true if no match was found, or false otherwise. Python synthax is pretty cool there.
>>and for the list splicing, so if i changed the order of the list it wouldnt work how i want by changing what message appears for what right?
Yep, but at least you have only one list to update instead of 3. Updating the splice values is much faster.
>>also wouldnt i need to flip the splices positive and negative to get it to be proper?
No, I checked it myself. name_gender_type[-3:] gets the last 3 element of the list, which are the male qualifiers.
0
what did you do and why does mine not work?
0
so .lower () made it where i dont have to do Male and male. got it. was the shortening of name_gender_type just for convenience?
i understand what the while loop does (checking if the input is in the list) but how?
and for the list splicing, so if i changed the order of the list it wouldnt work how i want by changing what message appears for what right?
also wouldnt i need to flip the splices positive and negative to get it to be proper?
0
oh no, the loop asking for input again is actual much better,because this isnt the only code, i need it to continue past this. im just working out some kinks i have, thank you! lol
what you did the first time will work fine because the error ends the code anyways, so they have to reenter.
0
okay thanks! im trying to make a version of that life game, but with more features and user input. im fairly new to coding though so im trying not to be super complex
i was kinda on the right track, but i havent mastered the order of items or all the possible commands lol
0
okay zen, new problem. trying to make it randomize a name from a pre determined list based on your selected gender. here's what i got.
name_male_list = ['Test_Male_One','Test_Male_Two']
name_female_list = ['Test_Female_One','Test_Female_Two']
male_name = random.choice(name_male_list)
print ("your name is " + male_name)
female_name = random.choice(name_female_list)
print ("your name is " + female_name)
i need to know how to pick a name from only one list depending on the gender. currently it tells you a name from both list
EDIT i figured it out. i used
if gender in name_gender_type[-3:]:
male_name = random.choice(name_male_list)
print ("your name is " + male_name)
elif gender in name_gender_type[:3]:
female_name = random.choice(name_female_list)
print ("your name is " + female_name)
but if i could simplify it help is nice.