The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
basic questions for a basic code
Discuss basic questions for a basic code in the Python Programming forum on Dev Shed. basic questions for a basic code Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 3rd, 2004, 06:08 AM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Posts: 103
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
|
|
|
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.
|

February 3rd, 2004, 08:01 AM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 325
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
|
|
|
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)
|

February 3rd, 2004, 08:11 AM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Posts: 103
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
|
|
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)
|

February 3rd, 2004, 08:11 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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
|

February 3rd, 2004, 08:24 AM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Posts: 103
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
|
|
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.')
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|