I'm a beginner at Python

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

with open('weather.txt', 'r', encoding='utf8') as weather_file:
    date = input ('What's the date today?')
    contents = weather_file.readlines()[int(date)]
    print(contents)
    If contents == 'Rain' or contents == 'Snow':
        print ('take your umbrella')
    elif contents == 'Clear':
        print ('You don't have to take care of anything')
    elif contents == '':
        print ('Weather Information')
    else:
        print ('I don't know')

I wanted to make something by myself, so I made it like this and entered the weather information in weather.txt, but the if statement does not respond to the weather I entered in the document What's the problem?

python

2022-09-20 15:09

1 Answers

I don't know what the weather.txt file is like.

Rain
The eyes
Sunny
Rain
Sunny

If the file looks like this, You must attach the opening letter \n if contents == 'Rain\n' or contents == 'Eye\n': Like this

If you want to use it universally, use find if contents.find('rain')!=-1 or contents.find('eye')!=-1: Write it like this


2022-09-20 15:09

If you have any answers or tips


© 2022 pinfo. All rights reserved.