|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
||||
|
||||
|
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 ![]()
__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!? Last edited by Boceifus : February 3rd, 2004 at 08:03 AM. |
|
#2
|
|||
|
|||
|
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)
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)
|
|
#3
|
||||
|
||||
|
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)
|
|
#4
|
||||
|
||||
|
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. |
|
#5
|
||||
|
||||
|
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.')
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > basic questions for a basic code |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|