943,833 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 6227
  • Python RSS
Feb 26th, 2009
0

how do i make this repeat? >.>

Expand Post »
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?

Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
StarZ is offline Offline
85 posts
since Dec 2008
Feb 26th, 2009
0

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

The standard way of repeating code is to use a while loop. There are two styles: the 'test first' and 'test last' methods.

Python Syntax (Toggle Plain Text)
  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

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: How to display images simultaneously in wxPython?
Next Thread in Python Forum Timeline: Advanced Mash: I'm stuck





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC