View Single Post
Hei. Jeg gjorde følgende:

1. Først legger jeg hele koden i en funksjon som heter "def_start_the_program"(gjøre det mer "pythonic" som de kaller det)
2. Så kaller jeg på denne funksjonen helt på slutten av programmet
3. Så fjerner jeg "continue" og kaller funksjonen istedenfor.

Funker fint her

her er koden:

Kode

# - Good Morning Program -

# Import
import datetime
import requests
import sys

def start_program():
    # 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':
            start_program()
        else:
            print('Goodbye')
            break
start_program()
Til admin "Vidarlo" og dere andre, hope you are watching (Kysse-emoji)
Sist endret av cplpro; 1. mai 2019 kl. 19:49.