Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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
Python 与if语句混淆_Python - Fatal编程技术网

Python 与if语句混淆

Python 与if语句混淆,python,Python,我写了这个程序,可以得到当前天气预报,然后告诉你是否应该带伞。 问题是: 天气预报=大部分时间晴朗,最高气温接近46度。微风习习,西风16至22英里/小时,阵风高达44英里/小时 因此,我的理解是if语句不应该执行,而应该直接转到else语句。但事实并非如此。任何帮助都将不胜感激 输出为: 大部分时间晴朗,最高气温接近46度。微风习习,西风16至22英里/小时,阵风高达44英里/小时 天气预报说有雨。将发送电子邮件提醒您要一把伞 进程已完成,退出代码为0 #Get weather from we

我写了这个程序,可以得到当前天气预报,然后告诉你是否应该带伞。 问题是:

天气预报=大部分时间晴朗,最高气温接近46度。微风习习,西风16至22英里/小时,阵风高达44英里/小时

因此,我的理解是if语句不应该执行,而应该直接转到else语句。但事实并非如此。任何帮助都将不胜感激

输出为:

大部分时间晴朗,最高气温接近46度。微风习习,西风16至22英里/小时,阵风高达44英里/小时

天气预报说有雨。将发送电子邮件提醒您要一把伞

进程已完成,退出代码为0

#Get weather from webpage
weatherToday = weather.find('div', class_='row row-odd row-forecast')
forecast = weatherToday.find('div',class_='col-sm-10 forecast-text').text
#print (weatherToday.prettify())
print(forecast)
print()

#Search through forecast to check for rain

#words = ['rain', 'showers']
#rain = forecast.find('rain')
#showers = forecast.find('showers')
if 'rain' or 'showers' in forecast:
    print('Forecast calls for rain. Will send an email to remind for an umbrella.\n')
    ezgmail.send('XXXXXX@gmail.com', 'Rain Forecast', 'Expected to rain. Pack an umbrella.')
    #percipitaion = True
else:
    print('Forecase does not call for rain. No reminder needed.\n ')
    ezgmail.send('XXXXXX@gmail.com', 'Rain Forecast', 'Not Expected to rain. No umbrella needed.')
    #percipitaion = False

如果天气预报中的“雨”或“阵雨”不能像您想象的那样工作

如果'rain'
计算结果为
True
,因为
'rain'
始终为True(thy)。您要执行以下操作:

如果预报中有“雨”或预报中有“阵雨”:
#代码。。。
请注意,您可以这样概括此包含:

如果有(天气预报中的天气值(“雨”、“阵雨”):
#代码。。。

这使您可以轻松地添加到
if
语句中所需的值集,而无需很长的条件。

在您的情况下,word
rain
True
,因为在布尔值转换过程中,任何非空字符串都将为True。也许,你的意思是:

words = ['rain', 'showers']
if any(word in forecast for word in words):
    ... # any code

看看,这回答了你的问题吗?根据我的理解,因为预测字符串不是空的,并且使用if语句调用字符串,所以它自动为True。但是in关键字不能阻止这一点吗?既然我是说如果天气预报有“雨”或“阵雨”,那就发电子邮件吧,如果没有,那就别发了。我只是没有正确理解这一点吗?