'How to make input correspond with a collection of values, and make a decision based on the input?
The goal here is to write a function called displayPerson that takes in an integer called id as its first parameter, and a dictionary as its second parameter, called personData.
The purpose of the function is to print the name and birthday of a given user identified by the input id. If there is no entry with the given id, then print “No user found with that id” instead.
The format should be “Person # id is name with a birthday of
date”, where id is the id # inputted, and name is the name of the person (from the file) and date is the birthday of the user (formatted as YYYY-MM-DD.
This is what I have so far
import argparse
import urllib.request
import datetime
import logging
#url https://s3.amazonaws.com/cuny-is211-spring2015/birthdays100.csv
#url = input('Insert URL here: ')
url = "https://s3.amazonaws.com/cuny-is211-spring2015/birthdays100.csv"
def downloadData(url):
response = urllib.request.urlopen(url)
data = response.read().decode('utf-8')
#print(data)
return data
def processData(file_content):
dictionary = {}
#print(file_content)
# [
# "1,Charles Paige,06/01/1963",
# "2,Andrew Bell,29/03/1972",
# ...
# "99,Alan Wilson,03/04/1960",
# "100,Austin Burgess,04/06/1979"
# ]
count = 0
data_items = file_content.splitlines()
logging.basicConfig(filename='error.log', filemode='w', level=logging.ERROR)
for line in data_items[1:]:
data_pieces = line
data_pieces = data_pieces.split(',')
# ["1", "Charles Paige", "06/01/1963"]
count = count + 1
#print(data_pieces)
# dictionary[data_pieces[0]] = (data_pieces[1]), datetime.datetime.strptime((data_pieces[2]), '%d/%m/%Y')
try:
dictionary[data_pieces[0]] = (data_pieces[1]), datetime.datetime.strptime((data_pieces[2]), '%d/%m/%Y')
except ValueError:
logging.error("Error processing line #: " + str(count) + " for ID #: " + str(data_pieces[0]))
return dictionary
def displayPerson(id, personData):
#print(personData)
#return
try:
id = input("ID:")
print("Person #" + id + "is" + dictionary[data_pieces[1]] + "with a birthday of" + datetime.datetime.strptime((data_pieces[2]), '%Y-%m-%d'))
except:
print("No user ID found")
def main():
downloadData(url)
file_content = downloadData(url)
values = processData(file_content)
#print(values)
displayPerson(id, values)
When I input an ID number, it raises the except every time. I'm not sure how to format the code to correspond the ID number with the values from the dictionary I created in processData.
Solution 1:[1]
Your code all seems to work OK except for the print line in your displayPerson function. Replace that line with this, and I think you'll get the behavior you're looking for:
print("Person #" + id + " is " + personData[id][0] + " with a birthday of " + personData[id][1].strftime('%d/%m/%Y'))
When I enter a value of "1", I get the following output:
Person #1 is Charles Paige with a birthday of 06/01/1963
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CryptoFool |
