Python import time import pandas as pd import numpy as np import datetime as dt
ID: 3908480 • Letter: P
Question
Python
import time
import pandas as pd
import numpy as np
import datetime as dt
def get_city():
# Ask the user for a city and returns the filename for that city's
citylist=[ 'Texas', 'California' , 'Washington']
while name in citylist:
city= input( 'Hello! Let's explore some US bikeshare data! Would you like to see data for Texas, California, or Washington? ')
if name=='Texas':
return 'texas.csv'
elif name == 'California':
return 'california.csv'
elif name == 'Washington':
return 'washington.csv'
else:
print('Try again')
def statistics():
# Filter by city ('Texas', 'California' , 'Washington')
city= get_city()
print(' Loading data')
df=pd.read.csv(city,parse_dates= ['Start Time' , 'End Time'])
restart = input(' Would you like to restart? Enter yes or no. ')
while restart.lower() != 'yes':
break
if __name__ == "__main__":
statistics()
What am I doing wrong in Python code?
Wish someone help me to find the answer.
Explanation / Answer
Mistakes:
1. Using name in while loop before declaring name
2. pd.read.csv is wrong. it is pd.read_csv
3. for restart you are using while where if condition is enough
Modified Code:
import time
import pandas as pd
import numpy as np
import datetime as dt
def get_city():
# Ask the user for a city and returns the filename for that city's
citylist=[ 'Texas', 'California' , 'Washington']
city = ""
while True:
city= input( 'Hello! Let's explore some US bikeshare data! Would you like to see data for Texas, California, or Washington? ')
if city=='Texas':
return 'texas.csv'
elif city == 'California':
return 'california.csv'
elif city == 'Washington':
return 'washington.csv'
else:
print('Try again')
def statistics():
# Filter by city ('Texas', 'California' , 'Washington')
city= get_city()
print(' Loading data')
df=pd.read_csv(city,parse_dates= ['Start Time' , 'End Time'])
restart = input(' Would you like to restart? Enter yes or no. ')
if restart.lower() == 'yes':
statistics()
if __name__ == "__main__":
statistics()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.