I am trying to take user input (zipCode) and pass that into an API request. How do I format the zipCode user input so that the GET request reads it as a string.
JavaScript
x
24
24
1
import requests
2
zipCode_format = ''
3
4
def user_input():
5
zipCode = input('What is your zipcode? Zipcode: ')
6
zipCode_format = "zipCode"
7
return zipCode_format
8
9
def weather_report():
10
params = {
11
'access_key': #keyhere,
12
'query': zipCode_format,
13
'units': 'f'
14
}
15
16
api_result = requests.get('http://api.weatherstack.com/current', params)
17
18
api_response = api_result.json()
19
20
print(u'Current temperature in %s is %d degrees Farenheit.' % (api_response['location']['name'], api_response['current']['temperature']))
21
22
user_input()
23
weather_report()
24
As you can see above, I have tried to declare another variable that turns the zipCode into a string.
Advertisement
Answer
Firstly, function user_input()
returns which means that in the left side of function call you need to have a variable in which you pass return from function. Secondly, you need to pass this variable to a function.
The third thing is that input()
outcome is a string even if you provide number.
JavaScript
1
5
1
zipCode = input("Provide zipCode")
2
>> 23-312
3
print(type(zipCode))
4
>> <class 'str'>
5
Snippet based on your code
JavaScript
1
20
20
1
import requests
2
3
def user_input():
4
zipCode = input('What is your zipcode? Zipcode: ')
5
return zipCode
6
7
def weather_report(zipCode):
8
params = {
9
'access_key': #keyhere,
10
'query': zipCode,
11
'units': 'f'
12
}
13
api_result = requests.get('http://api.weatherstack.com/current', params)
14
api_response = api_result.json()
15
16
print(u'Current temperature in %s is %d degrees Farenheit.' % (api_response['location']['name'], api_response['current']['temperature']))
17
18
zipCode = user_input()
19
weather_report(zipCode)
20