import pywapi
import string
zip = input(" ZIP Code: ")
cs = input("City, State: ")
weather_com_result = pywapi.get_weather_from_weather_com('zip')
print "Weather.com says: It is " + .lower(weather_com_result['current_conditions']['text']) + " and " + weather_com_result['current_conditions'] + ['temperature'] + "F now in" ['cs'] + "./n/n"
It gives me an error with 'cs'. Anyone know why?
-
Sailormanenzo: There is no need to leave thanks in the question. If there is an answer which best helped you to a solution, indicate it by ticking the large checkmark next to it on the left.goldilocks– goldilocks2016年09月07日 17:37:54 +00:00Commented Sep 7, 2016 at 17:37
-
Oh yes I see it now.LilVinny– LilVinny2016年09月07日 18:03:12 +00:00Commented Sep 7, 2016 at 18:03
2 Answers 2
Change your print statement like below (I mean the format) instead of using string concat operation. I would not say its not correct to write that way but the below way is always a better operation since you will have the control on string type as well.
import pywapi
import string
zip = input(" ZIP Code: ")
cs = raw_input("City, State: ")
weather_com_result = pywapi.get_weather_from_weather_com('zip')
print "Weather.com says: It is %s and %s F now in %s \n \n." %(str(lower(weather_com_result['current_conditions']['text'])), str(weather_com_result['current_conditions']['temperature']), str(cs))
However if you still need it in the concat way use the below sample syntax.
import pywapi
import string
zip = input(" ZIP Code: ")
cs = raw_input("City, State: ")
weather_com_result = pywapi.get_weather_from_weather_com('zip')
print "Weather.com says: It is ." + str.lower('abc') + " and " + 'def' + 'ghi' + "F now in " + str('ijk') + ".\n\n"
Errors in your program
Your print statement has a variable
weather_com_result['current_conditions'] + ['temperature'] it should be weather_com_result['current_conditions']['temperature']
Let me know if you have any questions.
-
When I use the first option I get:
Traceback (most recent call last): File "weather1.py", line 5, in <module> cs = input("City, State: ") File "<string>", line 1, in <module> NameError: name 'NYC' is not defined
LilVinny– LilVinny2016年09月09日 02:27:18 +00:00Commented Sep 9, 2016 at 2:27 -
Also on the second optionLilVinny– LilVinny2016年09月09日 17:39:00 +00:00Commented Sep 9, 2016 at 17:39
-
Try using raw_input instead of input. Let me know if you are still having issues, Looks like pywapi is something custom that you have written. So I did not test the whole stuff. Just looked at the traceback.Varad A G– Varad A G2016年09月10日 05:58:26 +00:00Commented Sep 10, 2016 at 5:58
-
Yes pywapi is a program made by someone from GitHubLilVinny– LilVinny2016年09月10日 18:52:40 +00:00Commented Sep 10, 2016 at 18:52
-
Traceback (most recent call last): File "weather.py", line 8, in <module> print "Weather.com says: It is %s and %s F now in %s \n \n." %(str(lower(weather_com_result['current_conditions']['text'])), str(weather_com_result['current_conditions']['temperature']), str(cs)) NameError: name 'lower' is not defined
LilVinny– LilVinny2016年09月10日 22:52:29 +00:00Commented Sep 10, 2016 at 22:52
You must replace ['cs']
with + cs
.
For the formatting, you can use the %
syntax. Then, you will not have errors like this.