I'm creating a project that has two lists of popular boy and girl names. it instructs the user to enter a boy a girl name and then lets them know if that name was popular. I'm pretty sure I have everything right, but I keep on getting a syntax error in line 18 (elif statement under if gender == 'g'), highlighting the colon after elif. If anyone has any idea why that would be very helpful because i can't figure it out
def main():
# Open a file for reading.
infile = open('GirlNames.txt', 'r')
infile2 = open('BoyNames.txt', 'r')
# Read the contents of the file into a list.
GirlNames = infile.readlines()
BoyNames = infile2.readlines()
# Close the file.
infile.close()
gender = input('Enter g to search for a girl name, b for a boys name, or both for both')
if gender == 'g':
girl = input('Please enter a girls name: ')
if girl in GirlNames:
print (girl, "was a popular girl's name between 2000 and 2009")
elif:
print (girl, "was not a popular girl's name between 2000 and 2009")
if gender == 'b':
boy = input('Please enter a boys name: ')
if boy in BoyNames:
print (boy, "was a popular boy's name between 2000 and 2009")
elif:
print (boy, "was not a popular boy's name between 2000 and 2009")
elif gender == 'both':
girl1 = input("Please enter a girl's name ")
boy1 = input("please enter a boy's name ")
if girl1 in GirlNames:
print (girl1, "was a popular girl's name between 2000 and 2009")
if boy1 in BoyNames:
print (boy1, "was a popular boy's name between 2000 and 2009")
# Call the main function.
main()
Related
I am new to Python and writing a simple program that is basically just an interactive conversation. I am asking the following question:
What is your name?
What is your gender?
What is your favorite color?
What is your birth year?
Do you have any pets?
etc.
The idea is to create as many response variables for each question as I can. For example: for "what is your gender?" if the user enters male, I want the response to be "Nice to meet you dude!", and if the user enters female I want the response to be "Nice to meet you miss!"
I would also like for the question "What is your birth year" to be met with a response that says You're "age"? You are so old!" (or something similar).
Below is what I have so far:
name = input('What is your name? ')
print('Hi ' + name + '!')
gender = input('What is your gender? ')
is_male = True
if is_male:
print('Nice to meet you, dude!')
else:
print('Nice to meet you, miss!')
favorite_color = input('What is your favorite color? ')
print(favorite_color + '? I love ' + favorite_color)
birth_year = input('What is your birth year? ')
age = 2021 - int(birth_year)
print(age + '? You are so old! ')
pets = input('Do you have any pets? ')
Any thoughts?
Thanks in advance!
I've found two bugs. Here, is_male is always True, so you'll always get the "dude" statement. And in case of age, age is int, so you can't concatenate int and str. Instead you can use fstring. Your corrected code:
name = input('What is your name? ')
print('Hi ' + name + '!')
gender = input('What is your gender? ')
if gender.lower()=="male" or gender.lower()=="m":
print('Nice to meet you, dude!')
else:
print('Nice to meet you, miss!')
favorite_color = input('What is your favorite color? ')
print(favorite_color + '? I love ' + favorite_color)
birth_year = input('What is your birth year? ')
age = 2021 - int(birth_year)
print(f'{age}? You are so old! ')
pets = input('Do you have any pets? ')
You have to convert an integer to string using str() when you concatenate a string with an integer.
age = 2021 - int(birth_year)
print('Your ' +str(age) + '? You are so old! ')
So you did most of it but you took an input
gender
but you did not use it
here:
is_male = True
if is_male:
this is_male is always true, you should read the input there instead of hardcoding it and give the user choices here like [M/F]
then you can continue like this
if gender == "M":
You might be looking for the elif statement like the following:
is_male = False
is_female = False
birth_year = input('What is your birth year? ')
age = 2021 - int(birth_year)
if age < 16:
print("Hello youngster.")
elif 16 <= age < 30:
print(r"Don't trust anyone over 30!")
elif 30 <= age < 50:
print("Metallica rules!")
else:
print("Take a nap. You deserve it.")
male_list = ["Bob", "Don","Joshua", "Cary", "Sachith" ]
female_list = ["Linda", "Tina","Janice", "Grace", "Stevie Nicks" ]
name = input('What is your name? ')
if name in male_list:
is_male = True
elif name in female_list:
is_female = True
if is_male:
print(f"Dude! Good to see you again {name}.")
elif is_female:
print(f"Good to see you again {name} my lady.")
else:
print(f'Nice to meet you {name}, we have not met.')
favorite_color = input('What is your favorite color? ')
print(f"{favorite_color} ? I love {favorite_color}")
I'm just writing some small code and I'm not able to make this work, I am only able to have one string which is e.g boy or girl but I would like to have both, do I have to make a separate elif statement or can I somehow put girl in there as well
gender = input("Are you a boy or a girl? : ")
if (gender != "boy"):
print("Get out of here you mutant")
exit()
You would need to have 3 conditions:
if gender == 'boy':
print ('You are a boy')
elif gender == 'girl':
print ('You are a girl')
else:
print("Get out of here you mutant")
Or you can do like that:
if gender in ('boy', 'girl'):
print('You are a {}'.format(gender)
else:
print("Get out of here you mutant")
In the second script you check if gender is a girl or a boy and if so it puts gender variable in the print output string.
I saw a line of code:
re.compile('[%s]' % re.escape(string.punctuation))
But I have no idea about the function of [%s]
Could anyone help me please?
Thank You!
It is a string formatting syntax (which it borrows from C).
Example:
name = input("What is your name? ")
print("Hello %s, nice to meet you!" % name)
And this is what the program will look like:
What is your name? Johnny
Hello Johnny, nice to meet you!
You can also do this with string concentation...
name = input("What is your name? ")
print("Hello + name + ", nice to meet you!")
...However, the formatting is usually easier.
You can also use this with multiple strings.
name = input("What is your name? ")
age = input("How old are you? ")
gender = ("Are you a male or a female? ")
print("Is this correct?")
print("Your name is %s, you are %s years old, and you are %s." % name, age, gender)
Output:
What is your name? Johnny
How old are you? 37
Are you a male or a female? male
Is this correct?
Your name is Johnny, you are 37 years old, and you are male.
Now, please, before asking any more questions, see if they exist!
Everything works in this program as intended except for two things. If you enter a boy name it will print everything correctly except it will add the print line "(name) was not ranked in the top 100 girl names...." and the same result when entering a girls name. Any thoughts on how to make it stop? Also the 'pos' that is returned is off by one-- is there a counter I can add to fix this?
try:
boyfile = open("boynames2014.txt", "r")
girlfile = open("girlnames2014.txt", "r")
boynames = [line.strip() for line in boyfile] #read the content to a list
girlnames = [line.strip() for line in girlfile] #read the content to a list
except IOError:
print("Error: file not found")
#Input gender
#search names in lists
gender = input("Enter gender (boy/girl): ")
if gender == "boy" or gender == "girl":
name = (input("Enter name to search for: "))
else:
print("Invalid gender")
if name in boynames:
pos = boynames.index(name)
print(name, "was ranked #", pos, "in 2014 for boy names")
else:
print(name, "was not ranked in the top 100 boy names for 2014")
if name in girlnames:
pos = girlnames.index(name)
print(name, "was ranked #", pos, "in 2014 for girl names")
else:
print(name, "was not ranked in the top 100 girl names for 2014")
boyfile.close()
girlfile.close()
You are checking both boy and girl names regardless of gender, you can add another condition to check gender.
if gender == "boy" and name in boynames:
pos = boynames.index(name) + 1
print("{} was ranked #{} in 2014 for boy names".format(name, pos))
elif gender == "girl" and name in girlnames:
pos = girlnames.index(name) + 1
print("{} was ranked #{} in 2014 for girl names".format(name, pos))
else:
print("{} was not ranked in the top 100 {} names for 2014".format(name, gender))
As for pos being 1 off, this is because lists are 0 indexed. Meaning the first item starts at index 0, second at index 1 and so on. so if your ranking starts at #1 then you just need to offset it by 1.
pos = boynames.index(name) + 1
Finally I would suggest you close your files inside the try/except after reading as well since you don't use them anywhere else in the code there is no need to leave them open.
You are checking name against both boynames and girlnames no matter what the gender was.
I'd suggest adding a conditional:
if gender == "boy":
if name in boynames:
pos = boynames.index(name)
print(name, "was ranked #", pos, "in 2014 for boy names")
else:
print(name, "was not ranked in the top 100 boy names for 2014")
elif gender == "girl"
if name in girlnames:
pos = girlnames.index(name)
print(name, "was ranked #", pos, "in 2014 for girl names")
else:
print(name, "was not ranked in the top 100 girl names for 2014")
I am trying to complete two different questions but cannot get them to work. Please help me understand where I went wrong.
1) For each number between 1 and 100, odds should be normal and even numbers should print out the word "Billy". Must start at 1 not 0 and include the number 100. Here's my answer (I know I'm way off)
for i in range(1,101):
if i % 2 == 0:
print(Billy)
else:
print(i)
2) Ask the user: "What is your name?". Response should look like "Hello Billy" for all names except Joe and Susie. For Joe it should say "Hi Joe :)" and for susie it should say "Ahoy Susie :D". Here is where I'm at:
name = input("What is your name?")
if name == "Joe":
print("Hi Joe :)")
if name == "Susie":
print("Ahoy Susie :D)
else: print("Hello", name)
try this
for i in range(1,101):
if i % 2 == 0:
print('Billy') #you missed quote marks here
else:
print(i)
(bad indentation, and missing quote marks)
and
name = input("What is your name?")
if name == "Joe":
print("Hi Joe :)")
elif name == "Susie":
print("Ahoy Susie :D") #and you missed quote marks here
else:
print("Hello" + name)
...same issues.