-3

In a program, you can simulate a magic square by using a two-dimensional list. Write a Python program that tests whether or not a 3 by 3 grid is a valid magic square. The program reads the input from a text file named input.txt. Each line in the file contains exactly 9 numbers that correspond to the the second row, and the last three values correspond to the last row.

I am not struggling with how to create the program to check to see if the square is a magic square, but how do I read in the "input.txt" into the program when I run it?

1 Answers1

0

If you're looking only for how to read the input.txt file, you can just do this:

f = open('input.txt', 'r'):
f.read()

Provided your input.txt file is in current path.

0xInfection
  • 2,676
  • 1
  • 19
  • 34
  • When I run the program do I need to save the text file in a specific place for the program to incorporate it when I run it? – Noah Henkle Jan 30 '19 at 01:33
  • Yes, it must be in the same folder/directory where your python file is. But if you want to include your text file from another directory, just replace `'input.txt'` in code with full path to the directory. – 0xInfection Jan 30 '19 at 01:35
  • Also you can do a [`os.cwd()`](https://www.tutorialspoint.com/python3/os_getcwd.htm) to get the current directory path. – 0xInfection Jan 30 '19 at 01:37
  • I am very new to python :(. How do I check if the two are in the same directory? – Noah Henkle Jan 30 '19 at 01:37
  • You don't need python to know if they are in same folder. Just put your text file in the folder where your python file is. I see the word 'directory' is confusing you. – 0xInfection Jan 30 '19 at 01:39
  • thanks man I was making it far too complicated... – Noah Henkle Jan 30 '19 at 01:46
  • @NoahHenkle, since you're new to this platform, I suggest have a look at [this](https://meta.stackexchange.com/a/5235/452257), so that people with similar questions might find your question helpful. – 0xInfection Jan 30 '19 at 01:48