I would like to get help with this program I have made. The function of the code is so the users can input a city anywhere in the world and they will then get data about the weather for that city.

I want it to restart the program from the top, but it only restarts the program from where the results are.

# - Weather Program -

#Import
import datetime
import requests
import sys

#Input
name_of_user = input("What is your name?: ")
city = input('City Name: ')

#API
api_address='http://api.openweathermap.org/data/2.5/weather?appid=0c42f7f6b53b244c78a418f4f181282a&q='
url = api_address + city
json_data = requests.get(url).json()

#Variables
format_add = json_data['main']['temp']
day_of_month = str(datetime.date.today().strftime("%d "))
month = datetime.date.today().strftime("%b ")
year = str(datetime.date.today().strftime("%Y "))
time = str(datetime.datetime.now().strftime("%H:%M:%S"))
degrees = format_add - 273.15
humidity = json_data['main']['humidity']
latitude = json_data['coord']['lon']
longitude = json_data['coord']['lat']

#Loop
while True:

    #Program
    if degrees < 20 and time > str(12.00):
        print("\nGood afternoon " + name_of_user + ".")
        print("\nThe date today is: " +
              day_of_month +
              month +
              year)
        print("The current time is: " + time)
        print("The humidity is: " + str(humidity) + '%')
        print("Latitude and longitude for " + city + " is: " + str(latitude), str(longitude))
        print("The temperature is a mild " + "{:.1f}".format(degrees) +
              "°C, you might need a jacket.")

    elif degrees < 20 and time < str(12.00):
        print("\nGood morning " + name_of_user + ".")
        print("\nThe date today is: " +
              day_of_month +
              month +
              year)
        print("The current time is: " + time)
        print("The humidity is: " + str(humidity) + '%')
        print("Latitude and longitude for " + city + " is: " + str(latitude), str(longitude))
        print("The temperature is a mild " + "{:.1f}".format(degrees) +
              "°C, you might need a jacket.")

    elif degrees >= 20 and time > str(12.00):
        print("\nGood afternoon " + name_of_user + ".")
        print("\nThe date today is: " +
              day_of_month +
              month +
              year)
        print("The current time is: " + time)
        print("The humidity is: " + str(humidity) + '%')
        print("Latitude and longitude for " + city + " is: " + str(latitude), str(longitude))
        print("The temperature is a warm " + "{:.1f}".format(degrees) +
              "°C, don't forget to drink water.")

    elif degrees >= 20 and time < str(12.00):
        print("\nGood morning " + name_of_user + ".")
        print("\nThe date today is: " +
              day_of_month +
              month +
              year)
        print("The current time is: " + time)
        print("The humidity is: " + str(humidity) + '%')
        print("Latitude and longitude for " + city + " is: " + str(latitude), str(longitude))
        print("The temperature is a warm " + "{:.1f}".format(degrees) +
              "°C, don't forget to drink water.")

    #Loop
    restart = input('Would you like to check another city (y/n)?: ')
    if restart == 'y':
        continue
    else:
        print('Goodbye')
        sys.exit()

So this is what happens.. The loop only loops the question with the input and data already filled in.

What is your name?: Test
City Name: Oslo

Good afternoon Test.

The date today is: 01 May 2019 
The current time is: 20:23:36
The humidity is: 76%
Latitude and longitude for Oslo is: 10.74 59.91
The temperature is a mild 12.7°C, you might need a jacket.
Would you like to check another city (y/n)?: y

Good afternoon Test.

The date today is: 01 May 2019 
The current time is: 20:23:36
The humidity is: 76%
Latitude and longitude for Oslo is: 10.74 59.91
The temperature is a mild 12.7°C, you might need a jacket.
Would you like to check another city (y/n)?: n
Goodbye

Process finished with exit code 0

I want the code to loop from the top so I can press y so the program will ask me for another city to input.

5 Answers

0
C.Nivs On Best Solutions

You are never updating your values. Let's take a simpler example:

x = int(input("What is the number you choose? "))

while True:
    if x >3:
        print(x)
        continue
    else:
        break

If I run this, I will get x printed out forever if I choose, say 5. The code defining x will never be re-run because it's outside of the loop. To fix this, I can move my code for x into the while loop:

while True:
    x = int(input("What number do you choose? "))
    if x>3:
        print(x)
    else:
        break

This will run the code for x every time the loop executes, so x can now change. Applying this to your code:

# loop is now up near the top
while True:
    # You want these values to change on each iteration of the while
    # loop, so they must be contained within the loop
    name_of_user = input("What is your name?: ")
    city = input('City Name: ')

    #API
    api_address='http://api.openweathermap.org/data/2.5/weather?appid=0c42f7f6b53b244c78a418f4f181282a&q='
    url = api_address + city
    json_data = requests.get(url).json()

    #Variables
    format_add = json_data['main']['temp']
    day_of_month = str(datetime.date.today().strftime("%d "))
    month = datetime.date.today().strftime("%b ")
    year = str(datetime.date.today().strftime("%Y "))
    time = str(datetime.datetime.now().strftime("%H:%M:%S"))
    degrees = format_add - 273.15
    humidity = json_data['main']['humidity']
    latitude = json_data['coord']['lon']
    longitude = json_data['coord']['lat']

    if degrees... # rest of your statements

Now the value for City can change, and you can apply that to the other data structures as well

0
TheDude On

You need to put a while loop right under the import statements.

0
Amit Botzer On

Put your while loop starting above the first input. Don't forget to declare your constant variables above the main loop.

0
Y. Archer On

The while loop only covers the code that display the results, the code that takes input and request the results is only executed once. You need to have everything except the import statements within the while loop

0
Pheelie C On

All you need to is put the input section and the API sections inside your while true loop.

I will say, I was unable to actually test my solution, but I am almost completely sure that it will work. Good luck!