I am trying to create a text based Monopoly game. For this particular section of my game, the program should be able to find the current space the user is on (which it already does) and use the space name to print off data correlating to the space from a separate text file.
eg. The player lands on Whitehall, the program looks for 'Whitehall' in the text file and prints off the whole line in the file containing 'Whitehall' eg. Whitehall; unowned; unmortgaged,
This is the code I currently have, but it doesn't seem to work:
current_space = 'Whitehall'
data = open('data.txt','r').read()
d_lines = data.split(',')
for d_lines in data:
if current_space in data:
print(d_lines)
else:
print('Not found')
I think it is reading all lines when Whitehall is found in the file but I don't know how to fix this.
I'm using Python 3.5. Here's a Pastebin link to my text file.
答案 0 :(得分:0)
you need to say:
d_lines = data.splitlines()
for line in d_lines:
if current_space in line:
print(line)
else:
print('Not found')
Right now your condition checks if current space is in data which i assume it will always be.