|
First Summer 2006 |
import random
good = 0
total = 0
one = random.randrange(-50, 50)
two = random.randrange(-50, 50)
answer = raw_input("What is" + str(one) + "+" + str(two) + "? ")
if one + two == int(answer):
print "Very Good"
good = good + 1
total = total + 1
else:
print "Wrong answer"
total = total + 1
print "So far you have", good, "out of", totalWe discussed how we could do this by copy and paste, using a loop or using a method.
Preeti Misra then solved this problem for us:
time = raw_input ("Please enter the time in military format, ex- 09:34::")
x1 = time.split(":")
hr = x1[0]
min = x1[1]
x = 0
print time
#Check for the min, whether it is 59 or not
while (x < 10):
x = x+1
if (min == "59"):
min = "00"
if (hr == "23"):
hr = "00"
else:
hr = int(hr) + 1
hr = str(hr)
else:
min = int(min) + 1
min = str(min)
if (len(min) < 2):
min = "0" + min
if (len(hr) < 2):
hr = "0" + hr
print hr + ":" + minWe also discussed various other ways of achieving the same result.
Finally, we created a file and processed it with the following program:
input = open("data.txt", "r")
maxline = ""
for line in input:
if len(line) > len(maxline):
maxline = line
input.close()
print "Longest line was: (", maxline[0:-1], ")"The result was the longest line in the file, shown between parens.
The name of the file was data.txt.