| | |
how do i make this repeat? >.>
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2008
Posts: 65
Reputation:
Solved Threads: 1
yea, this is long.. but how do i make this repeat?
At the end it's suppose to ask "do you want to do this again, y for yes, n for no" where yes is repeat from the beginning and no is exit. I think i messed it up............................ someone help?
At the end it's suppose to ask "do you want to do this again, y for yes, n for no" where yes is repeat from the beginning and no is exit. I think i messed it up............................ someone help?
Python Syntax (Toggle Plain Text)
import random print "Number of the day:" def dieroll(): randomnumber = random.randrange(100)+1 return randomnumber def add2dieroll(): y = dieroll() + dieroll() return y #main z= add2dieroll() print dieroll(), "+", dieroll(), "= [", z, "]" print "-" *35 name=raw_input("Hello,what is your name?") def addyear(x): x2 = 2009 - x return x2 #main print "Hello", name x =input("What year were you born?:") x2 = addyear(x) print x, "-2009 =", x2 print "cool, you are", x2, "years old" randomquestion=raw_input("so, do you like buying things?") a=raw_input("What are 2 items that you bought this week?:") def add2sub2(x,y): x2 = x + 2.05 y2 = y + 2.05 return x2, y2 #main x = input("how much did it cost ") y = input("how about the other one?") x2, y2 = add2sub2(x,y) print x, " + 5%GST + 8%PST = ", x2 print y, " + 5%GST + 8%PST = ", y2 print "the total is: ", "$", x2 + y2 def dieroll(): question=raw_input("Here are some lottery numbers you can use (press enter key):") for each in range(6): randomnumber = random.randrange(100)+1 print randomnumber answer = 'y' while answer == 'y': dieroll() answer=raw_input("Do you want new numbers? (y or n)") print "okay bye" raw_input("\n\nPress the enter key to exit.")
Last edited by StarZ; Feb 26th, 2009 at 7:18 pm.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
The standard way of repeating code is to use a while loop. There are two styles: the 'test first' and 'test last' methods.
Jeff
Python Syntax (Toggle Plain Text)
# a test-first loop: set the sentinel and repeat until sentinel is false repeat = True while repeat: print "You lose!" if raw_input("Play again? (y/n) ") == "n": repeat = False
Python Syntax (Toggle Plain Text)
# a test-last method: repeat until the test fails. This method makes more sense to me but is frowned on by some purists. while True: print "You lose!" if raw_input("Play again? (y/n) ") == "n": break
Jeff
![]() |
Similar Threads
- How to make a site sticky? (Promotion and Marketing Plans)
- Burning .dmg files (OS X)
- Help with CSS - background extending to 100% (HTML and CSS)
- help with Java (Java)
- Lost ability to make directories (Windows NT / 2000 / XP)
- Need help with Java Assignment (Java)
- C++ menu problem (C++)
- how will i repeat my whole game program? (Java)
Other Threads in the Python Forum
- Previous Thread: How to display images simultaneously in wxPython?
- Next Thread: Advanced Mash: I'm stuck
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti apache approximation array basic beginner book builtin calculator chmod code converter countpasswordentry curved dan08 dictionaries dictionary dynamic examples excel file filename float format ftp function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion recursive redirect refresh scrolledtext software ssh stamp statictext statistics string strings table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame write wxpython





