Clifford Attaglo
Clifford Attaglo

Reputation: 35

solve it but still having error somewhere

A cashier distributes change using the maximum number of five-dollar bills, followed by one-dollar bills. Write a single statement that assigns num_ones with the number of distributed one-dollar bills given amount_to_change. Hint: Use %.

Sample output with input: 19
Change for $ 19
3 five dollar bill(s) and 4 one dollar bill(s)

this is how i solved it but am still getting error on some part

amount_to_change = int(input())

num_fives = amount_to_change // 5

''' Your solution goes here '''
num_ones = 19 % 5
print('Change for $', amount_to_change)
print(num_fives, 'five dollar bill(s) and', num_ones, 'one dollar bill(s)')

Upvotes: 1

Views: 24098

Answers (2)

Jacob Scannell
Jacob Scannell

Reputation: 1

Your primary issue is that you hardcoded the amount used to get the number of ones. You can fix this by replacing the 19 with the value the user input. In the code below, I calculate the number of one's by subtracting the number of five's from the total.

amount_to_change = int(input())
num_fives = amount_to_change // 5
num_ones = (amount_to_change - (num_fives * 5)) // 1

print('Change for $', amount_to_change)
print(num_fives, 'five dollar bill(s) and', num_ones, 'one dollar bill(s)')

Upvotes: 0

azro
azro

Reputation: 54148

You may not use a hard-coded 19 , but rather amount_to_change, both 5 and 1 dollar bills

amount_to_change = int(input("Enter an amount"))

num_fives = amount_to_change // 5
num_ones = amount_to_change % 5

print('Change for $', amount_to_change)
print(num_fives, 'five dollar bill(s) and', num_ones, 'one dollar bill(s)')

Upvotes: 3

Related Questions