NOTE: this is intro to computer science, python based, please do not use anythin
ID: 3752676 • Letter: N
Question
NOTE: this is intro to computer science, python based, please do not use anything beyound chapter three to solve this problem
book used: introduction to computer science using python, a computational problem solving focus
modify the coin change excersise program in section 3.4.6 so that the least possible number of coins must be entered. Usining fuctions // and **
This is the code that needs to be modifed
# Coin Change Exercise Program
import random
# program greeting
print('The purpose of this exercise is to enter a number of coin values')
print('that add up to a displayed target value. ')
print('Enter coins values as 1-penny, 5-nickel, 10-dime and 25-quarter')
print("Hit return after the last entered coin value.")
print('----------------')
# init
terminate = False
empty_str = ''
# start game
while not terminate:
amount = random.randint(1,99)
print('Enter coins that add up to', amount, 'cents, one per line. ')
game_over = False
total = 0
while not game_over:
valid_entry = False
while not valid_entry:
if total == 0:
entry = input('Enter first coin: ')
else:
entry = input('Enter next coin: ')
if entry in (empty_str,'1','5','10','25'):
valid_entry = True
else:
print('Invalid entry')
if entry == empty_str:
if total == amount:
print('Correct!')
else:
print('Sorry - you only entered', total, 'cents.')
game_over = True
else:
total = total + int(entry)
if total > amount:
print('Sorry - total amount exceeds', amount, 'cents.')
game_over = True
if game_over:
entry = input(' Try again (y/n)?: ')
if entry == 'n':
terminate = True
print('Thanks for playing ... goodbye')
Explanation / Answer
Here is the program for chekcing whether the least number of coins are entered
# Coin Change Exercise Program
import random
# program greeting
print('The purpose of this exercise is to enter a number of coin values')
print('that add up to a displayed target value. ')
print('Enter coins values as 1-penny, 5-nickel, 10-dime and 25-quarter')
print("Hit return after the last entered coin value.")
print('----------------')
# init
terminate = False
empty_str = ''
# start game
while not terminate:
amount = random.randint(1,99)
print('Enter coins that add up to', amount, 'cents, one per line. ')
game_over = False
total = 0
# Compute the minimum denominations for the given total
#lets maintain count for each denomination required for the total amount
quarter_count = amount//25
remaining_amount = amount%25
dime_count = remaining_amount//10
remaining_amount = remaining_amount%10
nickel_count = remaining_amount//5
remaining_amount = remaining_amount%5
penny_count = remaining_amount
while not game_over:
valid_entry = False
while not valid_entry:
if total == 0:
entry = input('Enter first coin: ')
else:
entry = input('Enter next coin: ')
if entry in (empty_str,'1','5','10','25'):
valid_entry = True
else:
print('Invalid entry')
if entry == empty_str:
# Verify all the counts have become zero
if quarter_count == 0 and dime_count == 0 and nickel_count == 0 and penny_count == 0:
print('Correct denominations!')
elif total != amount:
print('Sorry - you only entered', total, 'cents.')
else:
print('Sorry - Entered denominations are not minimum')
game_over = True
else:
# decrease the entered coin from the respective count
if entry == '25':
quarter_count -= 1
elif entry == '10':
dime_count -= 1
elif entry == '5':
nickel_count -= 1
else:
penny_count -= 1
total = total + int(entry)
if total > amount:
print('Sorry - total amount exceeds', amount, 'cents.')
game_over = True
if game_over:
entry = input(' Try again (y/n)?: ')
if entry == 'n':
terminate = True
print('Thanks for playing ... goodbye')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.