這是我的計劃,我的工作,但我需要的amount_denom
顯示與數$。我將如何完成這項工作?我需要修復打印聲明,但是如何?我是編碼新手,所以有時候我看不到明顯的東西。謝謝。
def main():
denominations = [20, 10, 5, 1, .25, .10, .05, .01]
used_denom = []
amount_denom = []
user_input = float(input('enter a dollar amount: '))
#Keep asking user to re-enter input until they enter a value (0-200)
while user_input < 0 or user_input > 200:
user_input = float(input('re-enter a dollar amount: '))
#Traverse the list to breakdown the user_input into denominations.
remainder = user_input
for d in denominations:
num_denom = int(remainder/d)
if num_denom > 0:
used_denom.append(d)
amount_denom.append(num_denom)
#Avoid dividing by a float (prevents .01 issue from occurring)
remainder = (remainder*100) % (d * 100)/100
#Traverse the amount_denom list and print the output to be formatted a certain way.
for i in range(len(amount_denom)):
print("{0: 2d}{1:8.2f}".format(amount_denom[i],used_denom[i]),end = "")
print("s" if amount_denom[i] > 1 else "")
main()
print("{0: 2d} ${1:8.2f}".format(amount_denom[i],used_denom[i]),end="")
這正說明我: https://gyazo.com/ff41f5c82567c71cca05340f23a51e98
發現,我只需要在8.2f改爲5.2f加入$和used_denom到我喜歡的格式。謝謝。
如果我輸入25:輸出需求例如,顯示1 $ 20 1 $ 5。 – Spencer456
你有沒有嘗試在格式字符串中放置一個'$'? –