我已经获得了一个只定义了主函数的程序,我必须添加其他函数才能使程序正常工作。我目前卡在那里我必须使硬币数量变成一美元金额,只显示美元而不是剩余的美分。 我真正想要的是帮助开始def get_dollars。任何事情都会非常感激。
def main():
pennies = get_input1("Enter pennies : ")
nickels = get_input2("Enter nickels : ")
dimes = get_input3("Enter dimes : ")
quarters = get_input4("Enter quarters : ")
print("You entered : ")
print("\tPennies : " , pennies)
print("\tNickels : " , nickels)
print("\tDimes : " , dimes)
print("\tQuarters : " , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $", total_value, sep="")
print("You have", dollars, "dollars and", left_over_cents, "cent(s)")
def get_input1(pennies):
pennies = input("Enter Pennies: ")
if int(pennies) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of pennies: '))
return pennies
def get_input2(nickels):
nickels = input("Enter nickels: ")
if int(nickels) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of nickels: '))
return nickels
def get_input3(dimes):
dimes = input("Enter dimes: ")
if int(dimes) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of dimes: '))
return dimes
def get_input4(quarters):
quarters = input("Enter quarters: ")
if int(quarters) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of quarters: '))
return quarters
def get_total(pennies, nickels, dimes, quarters):
amount_pennies = (int(pennies) * .01)
amount_nickels = (int(nickels) * .05)
amount_dimes = (int(dimes) * .10)
amount_quarters = (int(quarters) * .25)
def get_dollars(pennies, nickels, dimes, quarters):
main()
答案 0 :(得分:0)
这加上便士的价值,然后除以100得到美元
def get_dollars(pennies, nickels, dimes, quarters):
return ( int(pennies) + 5*int(nickels)
+ 10*int(dimes) + 25*int(quarters) )//100
因为//
是整数除法,所以剩下的剩余便士金额将被丢弃。
举个例子:
>>> get_dollars('104', '44', '10', '4')
5
def get_dollars_and_cents(pennies, nickels, dimes, quarters):
total = int(pennies) + 5*int(nickels) + 10*int(dimes) + 25*int(quarters)
return total // 100, total % 100
示例:
>>> get_dollars_and_cents('104', '44', '10', '4')
(5, 24)
或者,
>>> dollars, cents = get_dollars_and_cents('104', '44', '10', '4')
>>> print('dollars=', dollars, 'cents=', cents)
dollars= 5 cents= 24