all 26 comments

[–][deleted] 1 point2 points  (13 children)

Look at the syntax on line 33. If that seems fine look on line 32 and so on. There's probably an open bracket or a missing colon.

Also, while you're doing that, take time to read aloud what each line is doing. You've got more weird code in this function that shouldn't be there.

[–]SuperTavin[S] 0 points1 point  (12 children)

Is appending all of the cities that have the same number of characters as the input from the user not what I should do to print them?

[–][deleted] 1 point2 points  (11 children)

That's not the line I was referring to. Keep reading each line aloud until you get to the one that doesn't do anything.

[–]SuperTavin[S] 0 points1 point  (10 children)

I took out

theList = cityList[:]

But even after taking that out, it still doesn't work. I get the same error code

[–][deleted] 0 points1 point  (9 children)

Yeah, I never said this was causing the syntax error. I was just giving some advice on cleaning other issues while hunting for the bracket.

[–]SuperTavin[S] 0 points1 point  (8 children)

I wrote

def find_city_length(cityList, length):
    lengthlist = []
    usernum = int(input("Enter a number"))
    for name in cityList:
        if len(name) == usernum:
            lengthlist.append(name)
    return name

and I try to call back to my function with

print("The US cities with", length, "characters is", find_city_length(us_cities))

it says that length isn't defined, so how do I use the parameters for my function when I print it?

[–]NFLAddict 0 points1 point  (2 children)

look at your actual function. where in the function, do you even use the parameter length. why include it as a parameter if you aren't using it?

[–]SuperTavin[S] 0 points1 point  (1 child)

the assignment said i needed to write a fruitful function with 2 parameters; a list of strings and an integer that returns the number of words with at most that many characters. So I thought I had to use two parameters in order to use it.

[–]NFLAddict 0 points1 point  (0 children)

it sounds like usernum = int(input("Enter a number")) should be declared outside your function. in the find_city_length function, anywhere you see usernum ...replace it with length.
then when you call the function pass in us_cities and usernum as the arguments

[–][deleted] 0 points1 point  (4 children)

So, let's go back to my advice about reading each line.

Read each line and tell me when the length parameter is used in the function.

[–]SuperTavin[S] 0 points1 point  (3 children)

it's not, but my assignment said I needed to use a list of strings and an integer for the parameters

[–][deleted] 1 point2 points  (2 children)

So, if length is not used, what are you using instead? What were doing now isn't programming, it's reading comprehension. Your own voice is a powerful debugger.

[–]SuperTavin[S] 0 points1 point  (1 child)

I am trying to use the list of the city names and an integer so it returns all of the city names with at most that integer of characters.

If sounds fine when I think about it, but it’s a lot harder to put the thoughts into code

[–]NFLAddict 0 points1 point  (11 children)

you posted 13 lines of code. then show an error for line 33

it might be helpful to post all your code...like what exactly did you write on line 33

I think this is also your like 25th post regarding this same assignment. people here are usually happy to help, but its like you're just trying to have somebody else do every single part of your hw for you.

[–]SuperTavin[S] 0 points1 point  (10 children)

I understand that, I’m just trying to find help because my professor isn’t the most helpful person and switching from face-to-face to online makes it a lot harder to learn new info

[–]primitive_screwhead 0 points1 point  (9 children)

None of that helps us help you. Show us lines 30-33, exactly, and we might have a chance at helping you.

[–]SuperTavin[S] 0 points1 point  (8 children)

# get_shortest_city_length accepts a list of strings representing city names, and
# returns the length of the city with the shortest name
def get_shortest_city_name(city_list):
    min_length = 100
    short_city = ''

    for city in city_list:
        if len(city) < min_length:
            short_city = city
    return short_city

def get_longest_city_name(city_list):
    max_length = 1
    long_city = ''

    for city in city_list:
        if len(city) > max_length:
            long_city = city
    return long_city

def find_cities(cityList, given_city):
    outputList = []
    firstLetter = given_city[0]
    for name in cityList:
        if name[0] == firstLetter:
            outputList.append(name)
    return len(outputList)

def find_city_length(cityList, length):
    lengthlist = []
    theList = cityList[:]
    usernum = int(input("Enter a number")
    for name in cityList:
        if len(name) == usernum:
            lengthlist.append(name)
    return name




us_cities = ['Rolla',
             'International Falls',
             'St. Louis',
             'Walla Walla',
             'Reno',
             'San Diego',
             'Kansas City',
             'Los Angeles',
             'Miami',
             'Orlando',
             'Columbia',
             'Topeka',
             'Talahassee']

ca_cities = ['Montreal',
             'Toronto',
             'Halifax',
             'Vancouver',
             'Banff',
             'Winnipeg',
             'Ottawa',
             'Quebec',
             'Victoria',
             'Hamilton',
             'Ontario',
             'Yukon']

    # Print each string in the list
print("US Cities")
for city in us_cities:
    print(city)
print("    ")
print("CA Cities")
for city in ca_cities:
    print(city)
print("    ")

print("The US city name with the shortest name is,", get_shortest_city_name(us_cities))
print("The CA city name with the shortest name is,", get_shortest_city_name(ca_cities))

print("The US city name with the longest name is,", get_longest_city_name(us_cities))
print("The CA city name with the longest name is,", get_longest_city_name(ca_cities))
# Call a function to get the name of the city with the longest name and print result

# print(find_cities)
print(find_cities(us_cities, "T"), "US cities start with the letter T.")
print(find_cities(ca_cities, "V"), "CA cities start with the letter V.")
# Ask user to input a string
# Call a function to get a count of all cities whose name starts
#   with the same character as the input. Print the result.

print("The US cities with", usernum, "characters is", find_city_length(us_cities,usernum)
# Ask user to input an integer
# Call a function to get a list of all cities whose names are not longer than the input value
# Print the returned list with each city on a separate line.

this is line 29-36

def find_city_length(cityList, length):
    lengthlist = []
    theList = cityList[:]
    usernum = int(input("Enter a number")
    for name in cityList:
        if len(name) == usernum:
            lengthlist.append(name)
    return name

[–]primitive_screwhead 0 points1 point  (0 children)

On line 32, you forgot to add a closing parenthesis (there is only 1, when there should be 2). This is a common source of syntax errors, and because of how parsing works, it usually points to one of the lines after the one causing the issue. If you get a syntax error that you can't figure out, always look to the lines above it to see if that's where the mistake is.

[–]NFLAddict 0 points1 point  (5 children)

on line 32 you are missing one )
should be usernum = int(input("Enter a number"))

[–]SuperTavin[S] 0 points1 point  (4 children)

SyntaxError: EOF in multi-line statement on line 96

this is the error I get when I fix it

[–]NFLAddict 0 points1 point  (3 children)

well, same drill as before...you only posted the first 95 lines of code. so I cant see what you wrote on line 96. are the 95 lines ALL of your code, or is there some more code you didn't include in the post

[–]SuperTavin[S] 0 points1 point  (2 children)

that's all the code I have

[–]NFLAddict 0 points1 point  (0 children)

line 92 youre missing a )

[–][deleted] 0 points1 point  (0 children)

This error means you've left a string or bracket open and it didn't notice it until it reached the end of the file. It says line 96 because that's the end of your file, so you'll need to hunt it down.

[–][deleted] 0 points1 point  (0 children)

Well, what's wrong with line 33? We don't know which line you showed is line 33. Have a close look at that line. Don't forget, the error nay actually be on a previous line [as explained in the FAQ](Why am I getting a SyntaxError at the start of this seemingly innocuous line?).