I was wondering if there is a better way to write my code instead of stacking a bunch of if, else statements together. I feel like my code could indeed be shorter, but I have yet to gain that knowledge. Can anybody help me out?
def main():
choice = input("Enter 'boy', 'girl', or 'both':")
boynames = open('BoyNames.txt', 'r').read().splitlines()
girlnames = open('GirlNames.txt', 'r').read().splitlines()
if choice=='boy':
boyselection=input("Enter a boy's name:")
if boyselection in boynames:
print(boyselection, "was a popular boy's name between 2000 and 2009.")
else:
print(boyselection, "was not a popular boy's name between 2000 and 2009.")
elif choice=='girl':
girlselection=input("Enter a girl's name:")
if girlselection in girlnames:
print(girlselection, "was a popular girl's name between 2000 and 2009.")
else:
print(girlselection, "was not a popular girl's name between 2000 and 2009.")
else:
if choice=='both':
bothboy = input("Enter a boy's name:")
if bothboy in boynames:
print(bothboy, "was a popular boy's name between 2000 and 2009.")
else:
print(bothboy, "was not a popular boy's name between 2000 and 2009.")
if choice=='both':
bothgirl = input("Enter a girl's name:")
if bothgirl in girlnames:
print(bothgirl, "was a popular girl's name between 2000 and 2009.")
else:
print(bothgirl, "was not a popular girl's name between 2000 and 2009.")
boynames.close()
girlnames.close()
main()
CodePudding user response:
This is really something you have to do for each individual function/algorithm/snippet you work on. In your case, all the cases boil down too:
def checkIfPopularInGender(gender, popular_names)
name = input(f"Enter a {gender}'s name:")
negate = '' if name in popular_names else 'not '
print(name, f'was {negate}a popular {gender} between 2000 and 2009')
Then you just feed it the gender string ('both', 'girl', 'boy') with the appropriate list of names - boys_names, girls_names, boys girls_names
. Even better, keep these in a dictionary:
name_sets = {'boy': boynames, 'girl': girlnames, 'both': boynames girlnames}
Consider making the lists sets too. So you are left with
choice = input("Enter 'boy', 'girl', or 'both':")
checkIfPopularInGender(choice, name_sets[choice])
This will also allow you to much more easily add genders, which may be useful. The 'both' will need changing to all, and probably added in a separate line to make this completely flexible.
CodePudding user response:
I would use conditional if statements (also is there a reason for both?), see below for an example:
def main():
choice = input('Enter "boy", "girl", or "both"')
with open('boynames.txt') as f: boynames = f.readlines()
with open('girlnames.txt') as f: girlnames = f.readlines()
if choice == "boy" or choice == "both":
selection = input("Input boy's name")
popular = (selection in boynames)
print(f"{selection} was a {'popular' if popular else 'not popular'} boy's name between 2000 and 2008.")
if choice == "girl" or choice == "both":
selection = input("Input girl's name:")
popular = (selection in girlnames)
print(f"{selection} was a {'popular' if popular else 'not popular'} girl's name between 2000 and 2008.")
This is definitely not the most succinct way to do it, however I think it makes the most sense and is a good introduction to f-strings (format strings), conditional if statements, and with open statements (with open takes care of opening and closing the txt documents).
If you are still confused, I can elaborate on anything you need me to. Hope this helps :).
CodePudding user response:
The thing you generally want to do is make sure your code doesn't repeat itself. There are 1000s of ways how to do so, and depending on what you want to do it's your choice on how to do it.
If you are writing code and see you have 2 lines which more or less do the same, there's bound to be a way how you can do it more efficiently!
In the case of your code, I'd do it something like this (note: not an expert myself):
def main():
gender_list = ['boy', 'girl']
choice = input("Enter 'boy', 'girl', or 'both':")
for gender in gender_list:
names = open(f'{gender.capitalize()}Names.txt', 'r').read().splitlines()
if gender == choice or choice == 'both':
name = input(f"Enter a {gender}'s name:")
addition = 'not ' if name not in names else ''
print(f"{name} was {addition}a popular {gender}'s name between 2000 and 2009.")
CodePudding user response:
Try this, as a starting point.
def main():
choice = input("Enter 'boy', 'girl', or 'both':")
with open("BoyNames.txt", "r") as boy_names_file:
boynames = boy_names_file.read().splitlines()
with open("GirlNames.txt", "r") as girl_names_file:
girlnames = girl_names_file.read().splitlines()
if choice in ["boy", "both"]:
boyselection=input("Enter a boy's name:")
print(f"{boyselection} was {'' if boyselection in boynames else 'not'} \
a popular boy's name between 2000 and 2009.")
if choice in ["girl", "both"]:
girlselection=input("Enter a girl's name:")
print(f"{girlselection} was {'' if girlselection in girlnames else 'not'} \
a popular girl's name between 2000 and 2009.")
Hope that helps.