title: Write a Python program that repeatedly asks the user to input coin values until the total amount matches a target value
body: <p>Write a Python program that repeatedly asks the user to input coin values until 
  the total amount matches a target value. The target value is a randomly 
  generated integer between 1 and 99 (inclusive). </p>
  
  <p>For example, for a target value of 31, coin values entered by the user could 
  be 25, 5, and 1 because 31 = 25 + 5 + 1. </p>
  
  <p>In order to generate a random number in Python for the target value, you have to 
  write the following statement at the start of your program: </p>
  
  <pre><code>import random 
  </code></pre>
  
  <p>You can then write the following statement in your program to generate a random 
  number: </p>
  
  <pre><code>rand = random.randint(1, 99) 
  </code></pre>
  
  <p>The above statement will generate a random number between 1 and 99 (both 
  end points are inclusive). This random number will be stored in the variable rand 
  (as per the above statement). You may use a different variable name if you want. 
  The random value stored in the variable named rand is the target coin value 
  that the user has to enter coins for. </p>
  
  <p>Sample Run of the Program </p>
  
  <p>On the following page is a complete sample run of the program. This is an 
  example of how the program will behave; in the following same, 59, 70 and 76 
  are randomly generated numbers. </p>
  
  <p>Example:
  The purpose of this exercise is to enter a number of coin values 
  that add up to a displayed target value. </p>
  
  <p>Enter coins values as 1-penny, 5-nickel, 10-dime, and 25-quarter. 
  Hit return after the last entered coin value. </p>
  
  <hr>
  
  <pre><code>Enter coins that add up to 59 cents, one per line. 
  Enter first coin: 
  Enter first coin: 3 
  Invalid entry 
  Enter first coin: 25 
  Enter next coin: 5 
  Enter next coin: 25 
  Enter next coin: 2 
  Invalid entry 
  Enter next coin: 1 
  Enter next coin: 1 
  Enter next coin: 1 
  Enter next coin: 1 
  Enter next coin: 
  Correct! 
  Try again (y/n)?: y 
  Enter coins that add up to 70 cents, one per line. 
  Enter first coin: 2 
  Invalid entry 
  Enter first coin: 25 
  Enter next coin: 25 
  Enter next coin: 10 
  Enter next coin: 5 
  Enter next coin: 1 
  Enter next coin: 25 
  Sorry - total amount exceeds 70 cents. 
  Try again (y/n)?: y 
  Enter coins that add up to 76 cents, one per line. 
  Enter first coin: 25 
  Enter next coin: 25 
  Enter next coin: 1 
  Enter next coin: 25 
  Enter next coin: 
  Correct! 
  Try again (y/n)?: n 
  Thanks for playing ... goodbye 
  </code></pre>
  
  <p>This is the code that I have so far:</p>
  
  <pre><code>import random
  
  def chkcoin(acoin):
      basecoin = ["1"," 5", "10", "25"]
      flag = False
      for bc in basecoin:
          if acoin == int(bc):
              return True
          else:
              flag = True
      if flag:
          print('Invalid entry')
          return False
  
  def tryAgain():
      comd = input('Try again (y/n)?: ')
      if comd == 'y':
          return True
      elif comd == 'n':
          print ('Thanks for playing ... goodbye')
          return False
      else:
          print ('Command error! Please enter y or n.')
          print ('Thanks for playing ... goodbye')
          return False
  </code></pre>
  
  <p>import random</p>
  
  <pre><code>def chkcoin(acoin): basecoin = ["1"," 5", "10", "25"] flag = False for bc in basecoin: if acoin == int(bc): return True else: flag = True if flag: print('Invalid entry') return False def tryAgain(): comd = input('Try again (y/n)?: ') if comd == 'y': return True elif comd == 'n': print ('Thanks for playing ... goodbye') return False else: print ('Command error! Please enter y or n.') print ('Thanks for playing ... goodbye') return False 
  </code></pre>
  
  <p>Can someone help me? i have 2question need to solve.</p>
  
  <ol>
  <li><p>when i need enter number. if i enter spacebar. this program will crash. i want if i enter spacebar, the result will same like 8 or another. "invalid try" </p></li>
  <li><p>this python will crash after one game. how to solve it? i write this python in python2. suddenly, i find i need write in python3. but i do not know how to change it. ty</p></li>
  </ol>