[Tutor] Reading number x and printing number x+1
Peter Otten
__peter__ at web.de
Fri Nov 22 10:23:41 CET 2013
G. McKinnon Ryall wrote:
> I have a script that outputs results to a file (one file, reused.) I would
> like to have an output file in this format
>
> #--------------------
> (blank line)
> (output from program (only one line))
> name
> (T/F)
> (result iteration, shortened to x.)
> #---------------------
> so like this
> #---------------------
>
> 55
> Joe
> false
> 1
>
> 96
> Bob
> true
> 2
>
> 28
> Mike
> true
> 3
> #---------------------
>
> I couldn't think of a way to read the last written number (it would be a
> multiple of 5).
Just read the lines from the file, convert the last line to an int and add
the new record. Example:
with open("tmp.txt", "a+") as f:
line = "0" # default when the file is empty or doesn't exist
for line in f:
pass
n = int(line) + 1
f.write("{}\n".format(n))
> I know how to add 1 and print. Also, how would I arrange
> #--------
> output.write("\n")
> output.write(x_string)
> output.write("\n")
> output.write(name)
> output.write("\n")
> output.write(exists_string)
> output.write("\n")
> #-------
> so that it didn't show up like this
> FIRST TIME
Now you have the n from my above sample you can run some of the code
conditionally:
if n == 1:
...
else:
...
> Also, how would I make this program better in general?
Long if elif sequences can sometimes be simplified with a dict lookup:
if a == 1:
print "alpha"
elif a == 2:
print "beta"
...
then becomes
messages = {1: "alpha", 2: "beta"}
try:
print messages[a]
except KeyError:
# handle cases not covered by the value lookup approach
Also, Python allows multiline strings:
f.write("alpha\n")
f.write("beta\n")
becomes
f.write("""\
alpha
beta
""")
> #BEGIN
> # Importing exists. Simple English. Nonsensical, but English nonetheless.
> from os.path import exists
> #Asking for your name
> name = raw_input("Hello!\nWhat is your name?\n\n")
> #Asking for the number and reacting to given response
> x = int(raw_input("\tFor the purposes of testing, please select a number
> between one and 100\n\n\t"))
>
> if x < 0:
> x = 4554073
> print "\t\tNo negatives, asshole. Number changed to '4554073' for
> 'ASSHOLE', asshole."
> elif x == 0:
> print "\t\tMore than zero."
> elif x > 100:
> x = 101
> print "\t\tDon't fuck with the computer."
> elif x == 42:
> print "\t\tThe meaning of life, the universe and everything."
> elif x == 7:
> print "\t\t7. How... creative. *cough* UNORIGINAL *cough*"
> elif x == 3:
> print "\t\t3! It's a magic numba\'. Yes it is! It's a magic numba\'."
> elif x == 37:
> print "\t\tThe two most common numbers."
> elif x == 99:
> print "\t\tI got 99 problems and a- wait, no, that's your IQ."
> elif x == 27:
> print "\t\tCONGRATULATATIONS! YOU'VE FOUND MY FOAVORITE NOMBER!"
> else:
> print "In all aspects your number appears to be normal."
> #Changing name to a string
> name_string = "%s" % name
> #Changing x to a string
> x_string = "%s" % x
> #Checking if file exists for archival purposes
> exists = exists("number_output.txt")
> exists_string = "%s" % exists
> #Opening output file
> number_output = 'number_output.txt'
> output = open(number_output, 'a')
> #Writing to file
> output.write("\n")
> output.write(x_string)
> output.write("\n")
> output.write(name)
> output.write("\n")
> output.write(exists_string)
> output.write("\n")
> #END
More information about the Tutor
mailing list