how do i make this repeat? >.>

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 65
Reputation: StarZ is an unknown quantity at this point 
Solved Threads: 1
StarZ StarZ is offline Offline
Junior Poster in Training

how do i make this repeat? >.>

 
0
  #1
Feb 26th, 2009
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?

  1. import random
  2.  
  3. print "Number of the day:"
  4.  
  5. print
  6. def dieroll():
  7. randomnumber = random.randrange(100)+1
  8. return randomnumber
  9.  
  10. def add2dieroll():
  11. y = dieroll() + dieroll()
  12. return y
  13.  
  14. #main
  15. z= add2dieroll()
  16. print dieroll(), "+", dieroll(), "= [", z, "]"
  17.  
  18. print "-" *35
  19. print
  20.  
  21. name=raw_input("Hello,what is your name?")
  22.  
  23. def addyear(x):
  24. x2 = 2009 - x
  25. return x2
  26.  
  27. #main
  28.  
  29. print
  30. print "Hello", name
  31. x =input("What year were you born?:")
  32. x2 = addyear(x)
  33. print
  34. print x, "-2009 =", x2
  35. print "cool, you are", x2, "years old"
  36.  
  37. print
  38. randomquestion=raw_input("so, do you like buying things?")
  39.  
  40. print
  41. a=raw_input("What are 2 items that you bought this week?:")
  42.  
  43. def add2sub2(x,y):
  44. x2 = x + 2.05
  45. y2 = y + 2.05
  46. return x2, y2
  47.  
  48.  
  49. #main
  50.  
  51. x = input("how much did it cost ")
  52. y = input("how about the other one?")
  53. print
  54. x2, y2 = add2sub2(x,y)
  55. print x, " + 5%GST + 8%PST = ", x2
  56. print y, " + 5%GST + 8%PST = ", y2
  57. print "the total is: ", "$", x2 + y2
  58.  
  59.  
  60.  
  61. def dieroll():
  62. question=raw_input("Here are some lottery numbers you can use (press enter key):")
  63. for each in range(6):
  64. randomnumber = random.randrange(100)+1
  65. print randomnumber
  66.  
  67. print
  68. answer = 'y'
  69. while answer == 'y':
  70. dieroll()
  71. answer=raw_input("Do you want new numbers? (y or n)")
  72.  
  73. print "okay bye"
  74. raw_input("\n\nPress the enter key to exit.")
Last edited by StarZ; Feb 26th, 2009 at 7:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: how do i make this repeat? >.>

 
0
  #2
Feb 26th, 2009
The standard way of repeating code is to use a while loop. There are two styles: the 'test first' and 'test last' methods.

  1. # a test-first loop: set the sentinel and repeat until sentinel is false
  2.  
  3. repeat = True
  4. while repeat:
  5. print "You lose!"
  6. if raw_input("Play again? (y/n) ") == "n":
  7. repeat = False

  1. # a test-last method: repeat until the test fails. This method makes more sense to me but is frowned on by some purists.
  2.  
  3. while True:
  4. print "You lose!"
  5. if raw_input("Play again? (y/n) ") == "n":
  6. break

Jeff
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC