Hardware Reference
In-Depth Information
CHALLENGE
If in your duplicator.py program, you type in a filename that does not exist,
the program crashes with an error and leaves your duplicating room in the
Minecraft world. Make your program more robust by researching on the Internet
how you can detect that a file does not exist so that your program does not crash.
When Martin tested the duplicator.py program for me, he found a bug—the
bug is that the room is actually bigger than it should be. This means that if you
build something the full size of the room, one slither of blocks down one edge will
get chopped off when it is scanned to the file, and when you print it, that slither
of blocks will be missing. This is quite inconvenient if it's the side of a house or
something! See if you can locate the cause of this bug and fix it yourself!
Quick Reference Table
Reading lines from a file
Writing lines to a file
f = open("data.txt", "r")
f = open("scores.txt", "w")
tips = f.readlines()
f.write("Victoria:26000\n")
f.close()
f.write("Amanda:10000\n")
for t in tips:
f.write("Ria:32768\n")
print(t)
f.close()
Getting a list of matching
filenames
Stripping unwanted white space
from strings
import glob
a = "\n\n hello \n\n"
names = glob.glob("*.csv")
a = a.strip()
for n in names:
print(a)
print(n)
Further Adventures in
Data.Files
In this adventure, you learnt how to read from and write to data files. This opens up
endless opportunities for saving and restoring parts of the Minecraft world, and even
bringing in large amounts of real-world data from other sources, such as websites. You
built your own 3D mazes with lots of winding tunnels and dead ends, and finished by
building a fully functional 3D scanner and printer, complete with a full menu system.
This technique of writing a menu system will be useful for many other programs too!
 
 
Search WWH ::




Custom Search