I made a program based on user input and I am using txt files with my program to check if the user input equal to the txt file.
my_age = open('c:\\file\\user_ask_about_my_age')
my_age_read = my_age.read()
user = raw_input("ask")
if user in my_Age_read:
print "I am 10 years old "
the txt file:
what is your age
how old are you
The problem is if the user types any char that is existed in my txt(ask_about...) the program print I am 10 years old
.
How to make my program read the txt file like string for each line?
I need a simple way and a small code to fix my problem because I have a lot of text files.
Make sure your other file is in the same directory, then on top of your script you can do add:
import myfile
If this file contains a function to print what you want to print, you can call this function with
myfile.print_function()
I think you can achieve what you ask this way:
with open('c:\\file\\user_ask_about_my_age', 'r') as fd:
lines = fd.readlines()
lines = [line.strip('\n') for line in lines]
user = raw_input("ask")
for line in lines:
if user == line:
print "i am 10 years old "
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.