Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要帮助使while循环从顶部重新启动我的Python程序吗_Python_Loops_While Loop - Fatal编程技术网

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

需要帮助使while循环从顶部重新启动我的Python程序吗,python,loops,while-loop,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

我想在我做的这个项目上得到帮助。代码的功能是,用户可以在世界任何地方输入一个城市,然后获得该城市的天气数据

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

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

我希望代码从顶部循环,这样我就可以按y键,程序就会要求我输入另一个城市。

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

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

x=int(输入(“您选择的数字是多少?”)
尽管如此:
如果x>3:
打印(x)
持续
其他:
打破
如果我运行这个,如果我选择,我将永远打印出
x
,比如
5
。定义
x
的代码永远不会重新运行,因为它在循环之外。要解决此问题,我可以将
x
的代码移动到
while
循环中:

为True时:
x=int(输入(“您选择什么数字?”)
如果x>3:
打印(x)
其他:
打破
这将在每次执行循环时运行
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
的值可以更改,您也可以将其应用于其他数据结构

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

只需将输入部分和API部分放入while true循环中即可


我会说,我无法实际测试我的解决方案,但我几乎完全相信它会起作用。祝你好运

continue
将转到
循环的开始处,而
循环。您希望重新询问的内容也需要循环,因此将它们移动到循环中移动您的
,而True:
位于城市输入上方。或者在名称的输入上方。同样,在这种情况下,您可以使用底部的
break
来退出,而不是
sys.exit()
Omg它可以工作!我将循环放置在城市下而不是名称下,因此它仅显示城市。完美,正如我所想!
# 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