119k views
0 votes
Write a function number_of_pennies() that returns the total number of pennies given a number of dollars and (optionally) a number of pennies.

Sample output with inputs: 5 6 4
506 400

''' Your solution goes here '''

print(number_of_pennies(int(input()), int(input()))) # Both dollars and pennies
print(number_of_pennies(int(input()))) # Dollars only

1 Answer

5 votes

Final answer:

To write the function number_of_pennies() that returns the total number of pennies given a number of dollars and (optionally) a number of pennies, you can follow these steps: calculate the total number of pennies from dollars, add any additional pennies if provided, and return the total number of pennies.

Step-by-step explanation:

To write a function number_of_pennies() that returns the total number of pennies given a number of dollars and (optionally) a number of pennies, you can use the following steps:

  1. First, calculate the total number of pennies from dollars by multiplying the number of dollars by 100.
  2. If there are also pennies given as input, add them to the total number of pennies.
  3. Return the total number of pennies.

Here is an example implementation of the function:

def number_of_pennies(dollars, pennies=0):
total_pennies = dollars * 100 + pennies
return total_pennies

# Example usage:
pennies_with_pennies = number_of_pennies(5, 6)
pennies_without_pennies = number_of_pennies(4506)
print(pennies_with_pennies)
print(pennies_without_pennies)

User Arowell
by
7.3k points