[Tutor] Reading number x and printing number x+1

G. McKinnon Ryall mckinnonryall at gmail.com
Fri Nov 22 06:57:30 CET 2013


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). 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

101
nope
True
1
SECOND TIME

101
nope
True
1
4554073
yup
True



Also, how would I make this program better in general?



#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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131121/fea8feef/attachment.html>


More information about the Tutor mailing list