#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11

    yes or no only loop [i]inside[/i] a loop


    greets

    i would like a better way to handle my verify response.if something other than "y" or "n" is used,to just repeat the verify question,instead of starting the whole input process again.thx in advance

    Code:
    import os.path
    
    fn= raw_input('Please enter the girls name. \n')
    
    if os.path.isfile(fn+'.txt'):
        print 'File found.'
        file = open(fn+'.txt', 'r')
        p=file.readline()
        l=file.readline()
        s=file.readline()
        print 'Phone Number:'+p
        print 'Location:'+l
        print 'Star Rating:'+s
        file.close()
    else:
        print 'No data on this girl!'
        print 'Please take a moment to enter her data. \n'
        while True:
            p= raw_input ('What is her phone number? \n')
            l= raw_input ('Where is she located? \n')
            s= raw_input ('How many stars? \n')
            print ' \nPLEASE VERIFY HER INFO: \n'
            print 'Name='+fn
            print 'Phone Number='+p
            print 'Location='+l
            print 'Star Rating='+s
            a= raw_input('\nIS THIS CORRECT? "y" FOR YES "n" FOR NO \n')
            if a == ('y'):
                print 'Sweet!Recording info!'
                file = open(fn+'.txt', 'w')
                file.write (p+'\n')
                file.write (l+'\n')
                file.write (s+'\n')
                file.close()
                break
            elif a == ('n'):
                print 'OK,lets try it again. \n'
            else:
                print 'Please answer "y" or "n".Re-initializing. \n'
                        
    print 'Future options coming soon.'
    raw_input('Press any key to quit.')
    p.s. no,i dont meet THAT many women...i plan to use this to make/search for config files that my future codes may need.the blackbook shnick just made it interesting
    Last edited by Boceifus; February 3rd, 2004 at 08:03 AM.
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12

    Re: basic questions for a basic code


    change this :
    Code:
            a= raw_input('\nIS THIS CORRECT? "y" FOR YES "n" FOR NO \n')
            if a == ('y'):
                print 'Sweet!Recording info!'
                file = open(' '+fn+'.txt', 'w')
                file.write (p+'\n')
                file.write (l+'\n')
                file.write (s+'\n')
                file.close()
                break
            elif a == ('n'):
                print 'OK,lets try it again. \n'
                time.sleep(1)
            else:
                print 'Please answer "y" or "n".Re-initializing. \n'
                time.sleep(1)
    to this
    Code:
            answer= raw_input('\nIS THIS CORRECT? "y" FOR YES "n" FOR NO 
            while answer != 'y' and answer != 'n':
                answer= raw_input('\nPLEASE TRY AGAIN: "y" FOR YES "n" FOR NO\n')
    
            if answer == 'y':
                print 'Sweet!Recording info!'
                file = open(' '+fn+'.txt', 'w')
                file.write (p+'\n')
                file.write (l+'\n')
                file.write (s+'\n')
                file.close()
                break
            elif answer == 'n':
                print 'OK,lets try it again. \n'
                time.sleep(1)
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    thank you very much Yogi!

    one small thing,you left off ') on your first line hehe.no biggie,it works after the syntax is fixed

    Code:
            answer= raw_input('\nIS THIS CORRECT? "y" FOR YES "n" FOR NO')
            while answer != 'y' and answer != 'n':
                answer= raw_input('\nPLEASE TRY AGAIN: "y" FOR YES "n" FOR NO\n')
    
            if answer == 'y':
                print 'Sweet!Recording info!'
                file = open(' '+fn+'.txt', 'w')
                file.write (p+'\n')
                file.write (l+'\n')
                file.write (s+'\n')
                file.close()
                break
            elif answer == 'n':
                print 'OK,lets try it again. \n'
                time.sleep(1)
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    or, totaly revamped...

    Code:
    #!/usr/bin/env python
    
    while True:
    	
    	name = raw_input('enter a Name to continue...')
    
    	if name:
    
    		path = 'names/' + name + '.txt'
    
    		try:
    			details = file(path, 'r').readlines()
    			print 'phone number...', details[0],
    			print 'location...    ', details[1],
    			print 'star rating... ', details[2],
    
    		except IOError:
    			print name, 'could not be found; add it?\n'
    
    			while True:
    				choices = raw_input('(Y/N)')
    				if choices == 'Y':
    					details = []
    					details.append(raw_input('phone number...') + '\n')
    					details.append(raw_input('location...    ') + '\n')
    					details.append(raw_input('star rating... ') + '\n')
    
    					file(path, 'w').writelines(details)
    
    					choices = 'N'
    
    				if choices == 'N':
    					break
    
    	print
    Looks for files in the current/names/ directory. most of this is formatting but hey

    You wrote a program so you can rate gurls you've been with original i'll give you that.

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    hehe,i edited my original post a few times,and i think you guys are so quick that you responded while i was editing hehehe.so my original is prob different that what you replied to hehe.

    but thank you for the fast replies!

    and im gonna give netytans approach a whirl hehe.and btw,i did mention (maybe wasnt isnt the version you read hehe) that i planned something else for this...just the blackbook theme seemed amusing

    here's the final code i came up with using Yogi's yes or no only code

    Code:
    import os.path
    
    fn= raw_input('Please enter the girls name. \n')
    
    if os.path.isfile(fn+'.txt'):
        print 'File found.'
        file = open(fn+'.txt', 'r')
        p=file.readline()
        l=file.readline()
        s=file.readline()
        print 'Phone Number:'+p
        print 'Location:'+l
        print 'Star Rating:'+s
        file.close()
    else:
        print 'No data on this girl!'
        print 'Please take a moment to enter her data. \n'
        while True:
            p= raw_input ('What is her phone number? \n')
            l= raw_input ('Where is she located? \n')
            s= raw_input ('How many stars? \n')
            print ' \nPLEASE VERIFY HER INFO: \n'
            print 'Name='+fn
            print 'Phone Number='+p
            print 'Location='+l
            print 'Star Rating='+s
            answer= raw_input('\nIS THIS CORRECT? "y" FOR YES "n" FOR NO')
            while answer != 'y' and answer != 'n':
                answer= raw_input('\nPLEASE TRY AGAIN: "y" FOR YES "n" FOR NO\n')
    
            if answer == 'y':
                print 'Sweet!Recording info!'
                file = open(fn+'.txt', 'w')
                file.write (p+'\n')
                file.write (l+'\n')
                file.write (s+'\n')
                file.close()
                break
            elif answer == 'n':
                print 'OK,lets try it again. \n'
                time.sleep(1)
    
                        
    print 'Future options coming soon.'
    raw_input('Press any key to quit.')
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?

IMN logo majestic logo threadwatch logo seochat tools logo