Python: need help (new to prog.)

Version_3

2[H]4U
Joined
Nov 19, 2007
Messages
4,076
Alright, I am trying to write a VERY simple script to perform a few things for my coworkers.

The script has a multifunction. For my team, it simply subtracts 'seconds' between the timestamps of 2 server backups (server qualification testing). For the data protection team, it will take the 2 'epoch' timestamps and convert them to a readable date/time field.

I wrote it to loop at the end, pending user interaction. My issue is, I want it to specifically only accept a 'y' or a 'n'. As it is in the current stage, it will dump out of the script if an 'n' is entered, but it will repeat if any other key is pressed, not just 'y'.

Also, if someone can let me know if there is a command that will pause it at the end and prompt for the enter key to close, otherwise, what I have works.

(note: I convert it to an exe so it runs in DOS on our laptops, so thats why there are so many "prints" so that there are some line separation)

Code:
import time

print 
print ""
print 
print "" 
print "" 
print 
print 

loop = True

while loop:
    bstate = True
    while bstate:
        try:
            begin = float(raw_input("Enter the beginning seconds: "))
            begin_string = time.gmtime(begin)
            print 
        except ValueError:
            print
            print "Not a valid entry. Try again."
            print
        else:
            bstate = False
    print 
    estate = True
    while estate:
        try:
            end = float(raw_input("Enter the end seconds (if no end time, enter 0): "))
            end_string = time.gmtime(end)
            print 
        except ValueError:
            print
            print "Not a valid entry. Try again."
            print
        else:
            estate = False
            
    difference = begin - end
    begin_struct = time.strftime('%a %m/%d/%Y, %I:%M:%S %p', begin_string)
    end_struct = time.strftime('%a %m/%d/%Y, %I:%M:%S %p', end_string)

    print 
    print 
    print "The difference in seconds is", difference 
    print 
    print 
    print "The beginning seconds translate to", begin_struct
    print 
    print "The end seconds translate to", end_struct
    print 
    print 
    question = raw_input("Do another? (y/n) ")
    restart = question.strip()
    if (restart == 'y'):
        loop = True
    elif (restart == 'n'):
        loop = False
else:
    print "Thank you for using this utility! Press enter to close."
    raw_input()
 
I wrote it to loop at the end, pending user interaction. My issue is, I want it to specifically only accept a 'y' or a 'n'. As it is in the current stage, it will dump out of the script if an 'n' is entered, but it will repeat if any other key is pressed, not just 'y'.
I think something like this, i haven't tested it.

Code:
valid_answer = False
loop = False #if you dont want to check for n specifically, make the default to not loop.
while not valid_answer:
  question = raw_input("Do another? (y/n) ")
      restart = question.strip()
      if (restart == 'y'):
          loop = True
          valid_answer = True
      elif (restart == 'n'):
          loop = False
          valid_answer = True

(note: I convert it to an exe so it runs in DOS on our laptops, so thats why there are so many "prints" so that there are some line separation)

you can use \n to signify a newline in a string, you you could shorten the start to something like:

Code:
print '\n\n\n\n\n\n\n'

Code:
else:
    print "Thank you for using this utility! Press enter to close."
    raw_input()

That should make you need to hit enter ? it seems to here.
 
Thanks, the raw_input at the end does what its supposed to, I just wasn't sure if there was a quicker way to do it.

I will try the code and see what happens. Thanks for getting back so fast! :)

Oh, and all those prints at the beginning are supposed to be there. where there are the 'quotes there is actually words that I enter. Version control, document revisions. This is a tool that will be used during validation, which the company i work at gets their systems audited under hipaa, fda and SOX. Need to have the info there so its traced back to the document I have to write that is done for verifying that this tool does what its supposed to. *sigh*
 
Well, That was simple! Yes, your suggestion works. It keeps prompting for y/n with any other letters. This is exactly as I wanted.
 
Back
Top