Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 27th, 2004, 04:59 PM
PA_Munk PA_Munk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 PA_Munk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Need some help if you please

Okay, first let me say I just started with Python and this is for a college class I'm taking. The progam below works just fine in the interactive window, however if I open the program in windows I input the selection and then the window closes RAPIDLY. I'd like to give the user an option to play again or to exit only I don't know how...can someone give me a hand?

Much appreciated!
Munk

# This program will determain if you are young or old!
print "welcome to the age program"
print "---------------------------------------"
print

# Print out the menu:
print "Please select an age group:"
print "1 16-20"
print "2 21-25"
print "3 26-30"
print "4 31-35"
print "5 36-40"
print "6 41-45"
print "7 46-50"
print "8 51-60"
print "9 61-70"
print "10 >70"


# Get the user's choice:
Choice = input ("> ")

# Return a response after user input:
if Choice == 1:
print "You are very Young!"
if Choice == 2:
print "You are still Young"
if Choice == 3:
print "Still young, but lookout at 30!"
if Choice == 4:
print "Middle age...sorry!"
if Choice == 5:
print "Pushing 40...now you're in trouble!"
if Choice == 6:
print "Over 40, not OLD but getting there"
if Choice == 7:
print "Okay now you are well on your way!"
if Choice == 8:
print "Life is almost over"
if Choice == 9:
print "You are officially OLD!"
if Choice == 10:
print "Over 70! You are older than DIRT!"

Reply With Quote
  #2  
Old November 27th, 2004, 05:47 PM
ivanhope ivanhope is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 35 ivanhope User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 59 m 4 sec
Reputation Power: 5
Code:
# This program will determain if you are young or old!
print "welcome to the age program"
print "---------------------------------------"
print

restart = "yes"

while restart != "no":
  print "\n\n", """Please select an age group:
  1-)  16-20
  2-)  21-25
  3-)  26-30
  4-)  31-35
  5-)  36-40
  6-)  41-45
  7-)  46-50
  8-)  51-60
  9-)  61-70
  10-)  >70"""

  answer = ["\nYou are very Young!", 
"\nStill young, but lookout at 30!", 
"\nMiddle age...sorry!", 
"\nPushing 40...now you're in trouble!", 
"\nOver 40, not OLD but getting there" , 
"\nOkay now you are well on your way!", 
"\nLife is almost over", "\nYou are officially OLD!", 
"\nOver 70!  You are older than DIRT!"]


  # Get the user's choice:
  option = int(raw_input ("> "))
  print answer[option - 1]
  
  restart = raw_input("\n\n\tWould you like to play again? >")
  restart = restart.lower()

Reply With Quote
  #3  
Old November 27th, 2004, 06:11 PM
ivanhope ivanhope is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 35 ivanhope User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 59 m 4 sec
Reputation Power: 5
You could just ask the user in what year did he born or how old is he and the program should calculate itself the age range

Reply With Quote
  #4  
Old November 27th, 2004, 06:21 PM
PA_Munk PA_Munk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 PA_Munk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I would however I have to stick to the assignment limitations. Okay different questions....is there a statement that I could put in that would keep the window from closing so the user could at least read the answer?

Thanks for the help ivanhope!

Reply With Quote
  #5  
Old November 27th, 2004, 08:01 PM
ivanhope ivanhope is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 35 ivanhope User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 59 m 4 sec
Reputation Power: 5
Quote:
Originally Posted by PA_Munk
I would however I have to stick to the assignment limitations. Okay different questions....is there a statement that I could put in that would keep the window from closing so the user could at least read the answer?

Thanks for the help ivanhope!


well in this case the program allways will ask if he wants to continue so it will wait untilo the user do something to close the window

you can write raw_input() just for wait til the user press enter

Reply With Quote
  #6  
Old November 28th, 2004, 06:36 PM
PA_Munk PA_Munk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 PA_Munk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ivanhope,

Can you explain to me the "n" and what it is/does in this program?

Thanks,
Munk

Reply With Quote
  #7  
Old November 28th, 2004, 07:19 PM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,282 Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 16 h 23 m 31 sec
Reputation Power: 618
Quote:
Originally Posted by PA_Munk
Ivanhope,

Can you explain to me the "n" and what it is/does in this program?


It is actually "\n" (a backslash followed by a lowercase n), and it stands for 'new line'. In Python (and several other languages, including C, Perl, and Java), the backslash character is used to mark what are called 'escape codes'. They are used to represent non-printable characters in strings. According to this page, Python supports the following character escape codes:
Code:
 \               Newline Continuation
 \\              Backslash
 \'              Single quote
 \"              Double quote
 \a              Bell
 \b              Backspace
 \e              Escape
 \n              Line feed
 \v              Vertical tab
 \t              Horizontal tab
 \r              Carriage return
 \0              Null
 \0XX            Octal character value
 \xXX            Hex character value 

in the last two cases, the 'XX' stands for two octal or hexadecimal digits; the value is used to insert the corresponding ASCII character. For example, '\x7F' would insert the ASCII DEL (delete) character.

Reply With Quote
  #8  
Old November 28th, 2004, 10:29 PM
PA_Munk PA_Munk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 PA_Munk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Need some help if you please


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT