View Single Post
Hei, er enda noob i python, vel koding generelt, starta for litt over 1 uke sia, men uansett. Skulle hatt hjelp til og loope et program til starten av programmet via input fra brukeren. y = restart, n = quit

Koden er:

Kode

# - Good Morning Program -

#Import
import datetime
import requests

#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')
        break

Det som skjer er:

Trykker y:

What is your name?: Hvagjordo
City Name: Oslo

Good afternoon Hvagjordo.

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

(Da looper den selve spørsmålet med svaret fra før, men ikke helt fra starten)

Trykker n

Good afternoon Hvagjordo.

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

Process finished with exit code 0




Så, n funker, men y looper ikke heeeeelt fra starten, men looper med all infoen og spør om man vil loope på nytt. Vil at den skal loope så jeg kan spørre om en annen by

Har googla, men ikke funnet frem til noe som fungerer