I have a program that asks for queries. It is based on a CSV file with three columns for a person's name, city, and height. The print statement for City_list gives it a blank. The problem I'm running into is that if I insert a city that isn't in the CSV file, it ends and doesn't transition to other cities. When I do put in the city that is in the list, it prints out the indexes from the file. How can this be revised to ignore off-list cities and work with in-list cities?   Example Output: C:\Users\lucas\PycharmProjects\pythonProject3\venv\Scripts\python.exe C:\Users\lucas\Documents\cs1151\ch8\querydata.py  Welcome to my query program! What would you like to do?   1) Count how many people in a city:  2) Find the tallest person/people in a city. 0) Quit Option: 1 Which city?Ely The population of Ely is 10 What would you like to do?   1) Count how many people in a city:  2) Find the tallest person/people in a city. 0) Quit Option: 2 Which city?Ely [1, 12, 13, 14, 20, 25, 40, 65, 84, 90]

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I have a program that asks for queries. It is based on a CSV file with three columns for a person's name, city, and height. The print statement for City_list gives it a blank. The problem I'm running into is that if I insert a city that isn't in the CSV file, it ends and doesn't transition to other cities. When I do put in the city that is in the list, it prints out the indexes from the file. How can this be revised to ignore off-list cities and work with in-list cities?

 

Example Output:

C:\Users\lucas\PycharmProjects\pythonProject3\venv\Scripts\python.exe C:\Users\lucas\Documents\cs1151\ch8\querydata.py 

Welcome to my query program!

What would you like to do?

 

1) Count how many people in a city: 

2) Find the tallest person/people in a city.

0) Quit

Option: 1

Which city?Ely

The population of Ely is 10

What would you like to do?

 

1) Count how many people in a city: 

2) Find the tallest person/people in a city.

0) Quit

Option: 2

Which city?Ely

[1, 12, 13, 14, 20, 25, 40, 65, 84, 90]

 

# Lucas Conklin

# 5772707

import csv



def readCSVIntoDictionary(f_name):

    dictionary = dict()

    with open(f_name) as f:

        f_reader = csv.reader(f)

        headers = next(f_reader)

        id = 1

        for row in f_reader:

            item_dictionary = dict()

            for i in range(0, len(row)):

                item_dictionary[headers[i]] = row[i]

            dictionary[id] = item_dictionary

            id += 1

        f.close()

    return dictionary



def printItem(item):

    for k, v in item.items():

        print(f'  {k:20}: {v}')



def lookupIDs(mydict, key, value):

    matching_keys = []

    for i in mydict.keys():

        if mydict[i][key] == value:

            matching_keys.append(i)

    return matching_keys



def people_counter(dict, city):

    city_pop = lookupIDs(dict, "city", city)

    return len(city_pop)



def find_tallest(dict, city):

    city_dict = lookupIDs(dict, "city", city)

    print(city_dict)

 

    tall = 0

    for i in city_dict:

        height = int(dict[i]["height"])

        if height > tall:

            tall = height

 

    tallest = []

    for i in city_dict:

        height = int(dict[i]["height"])

        if tall == height:

            name = dict[i]["name"]

            tallest.append(name)

    while True:

        if city_dict == []:

            continue

    return tallest



cityDictionary = readCSVIntoDictionary("C:\\Users\\lucas\\Downloads\\sample-data.csv")

print("Welcome to my query program!")

 

while True:

    print("What would you like to do?")

    print()

    print("1) Count how many people in a city: ")

    print("2) Find the tallest person/people in a city.")

    print("0) Quit")

 

    option = int(input("Option: "))

    if option == 0:

        print("Goodbye!")

        break

    elif option == 1:

        city = input("Which city?")

        pop = people_counter(cityDictionary, city)

        if pop == 0:

            print(f'There are no people in {city}.')

        else:

            print(f'The population of {city} is {pop}')

    elif option == 2:

        city = input("Which city?")

        people_list = find_tallest(cityDictionary, city)

 

        if len(people_list) <= 1:

            print(f'The tallest people/person in {city} is {people_list[0]}')

        else:

            print(f'The tallest people/person in {city} are ')

            for people in people_list:

                print(f'  {people}  ')

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Lists
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education