0
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 Sep 7 '16 at 17:37
  • Oh yes I see it now. – LilVinny Sep 7 '16 at 18:03
1

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 Sep 9 '16 at 2:27
  • Also on the second option – LilVinny Sep 9 '16 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 Sep 10 '16 at 5:58
  • Yes pywapi is a program made by someone from GitHub – LilVinny Sep 10 '16 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 Sep 10 '16 at 22:52
2

You must replace ['cs'] with + cs.

For the formatting, you can use the % syntax. Then, you will not have errors like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.