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 May 21st, 2004, 12:53 AM
EmergencyBlack EmergencyBlack is offline
Diragono
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: SoCal
Posts: 2 EmergencyBlack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to EmergencyBlack Send a message via MSN to EmergencyBlack
A little help (Just starting!)

Sorry if this seems a bit stupid, but I'm just learning Python (as a first programming language, too). Anyways, here it goes. I'm writing a simple little program like this:

Code:
import sys
print """
Choose a shape from the list:
1) Triangle
2) Square
3) Circle
4) Quit
"""
shape = raw_input("Choose an option[1,2,3,4]? ")
if shape == '1':
    ht = input('What is the height of your triangle? ')
    base = input('How long is the base? ')
    print "The triangle's area is: ", 05*base*ht

elif shape == '2':
    side = input('How long are the square sides? ')
    print "The square's area is: ", side*side

elif shape == '3':
    rad = input('What radius is your circle? ')
    print "The circle's area is: ", 3.14159*rad*rad

elif shape == '4':
    quit = sys.exit()

else:
    print "Sorry, you did not enter a valid choice."


But, what I want to know is how to make the program restart (so it presents you with the list of choices again) after you make your choice. Any help is greatly appreciated!

-Tim

Reply With Quote
  #2  
Old May 21st, 2004, 03:23 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
Try a while loop:

Code:
while True:
    #your code indented here


Grim
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #3  
Old May 21st, 2004, 03:35 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 2 m 16 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
If you put the body of your program inside a while True loop then your program will keep running untill you make a call to the break keyword i.e.

Code:
#!/usr/bin/env python

while True:

    choice = raw_input("1 or 2")

    if choice == '1':
        print 'you said one... You Win, YAY'
    elif choice == '2':
        print 'you said two... bad luck, bye'
        break
    else:
        print 'I said 1 or 2 dummy, try again'


Also, you should probably use int(raw_input()) over input since it is possable for input() to be used to executure arbitary code since input() is actually the same as doing eval(input()). Which is why you can do things like 1 + 2 though input().

Have fun,

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


Reply With Quote
  #4  
Old May 21st, 2004, 03:37 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 2 m 16 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Ooops, lucks like you beat me there Grim .

Mark.

Reply With Quote
  #5  
Old May 21st, 2004, 01:03 PM
EmergencyBlack EmergencyBlack is offline
Diragono
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: SoCal
Posts: 2 EmergencyBlack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to EmergencyBlack Send a message via MSN to EmergencyBlack
Ah, I knew it had something to do with loops, but I couldn't quite figure out how they worked. Thanks a lot.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > A little help (Just starting!)


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 3 hosted by Hostway