需要帮助以使while循环从顶部重新启动我的Python程序

时间:2019-05-01 19:38:11

标签: python loops while-loop

我想获得有关我制作的该程序的帮助。该代码的功能是使用户可以在世界任何地方输入城市,然后他们将获得有关该城市天气的数据。

我希望它从顶部重新启动程序,但只从结果所在的位置重新启动程序。

# - 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()

这就是发生的情况。.循环仅使用输入和已填充的数据来循环问题。

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

我希望代码从顶部开始循环播放,因此我可以按y,这样程序将要求我输入另一个城市。

5 个答案:

答案 0 :(得分:0)

您需要在import语句下放置一个while循环。

答案 1 :(得分:0)

将while循环置于第一个输入上方。不要忘记在主循环上方声明常量变量。

答案 2 :(得分:0)

您永远不会更新自己的价值观。让我们举一个简单的例子:

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

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

如果运行此命令,我会永久打印x,如果说5。定义x的代码将永远不会重新运行,因为它在循环之外。要解决此问题,我可以将x的代码移到while循环中:

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

这将在每次循环执行时为x运行代码,因此x现在可以更改。将此应用于您的代码:

# 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

现在City的值可以更改,您也可以将其应用于其他数据结构

答案 3 :(得分:0)

while循环仅覆盖显示结果的代码,接受输入并请求结果的代码仅执行一次。 您需要在while循环中包含除import语句之外的所有内容

答案 4 :(得分:0)

您需要做的就是将输入部分和API部分放入while true循环中。

我会说,我无法真正测试我的解决方案,但是我几乎完全确定它会起作用。祝你好运!