如何在一个项目中获得最终的总金额



此程序将询问用户拥有的半美元、25美分、1角硬币、5美分和1美分的数量,然后计算总价值。变化的总值既显示为总美分数,又显示为单独的美元和美分数。我想包括一个最终的总数,在用户完成后显示;其将显示在整个会话中输入的所有量的总和(即,所有循环迭代(。那么我该如何编码呢?

#getCoin function
def getCoin(coinType):
c = -1
while c < 0:
try:
c = int(input("How many " + coinType + " do you have? "))
if c < 0:
print("Coin counts cannot be negative. Please re-enter.")
except  ValueError:
print("llegal input. Must be non-negative integer. Re-enter.")
c = -1

return c
print("Welcome to the Change Calculator")
print()
choice = input ("Do you have any change (y/n)?")
while choice.lower() == "y":
h = getCoin("Half-Dollars")
q = getCoin("Quarters")       
d = getCoin("Dimes")
n = getCoin("Nickel")
p = getCoin("Pennies")
print()
TotalVal = (h*50) + (q*25) + (d*10) + (n*5) + p
print("You have " + str(TotalVal) + " cents.")
dollars = TotalVal // 100   # // is for division but only returns whole num
cents = TotalVal % 100      # % is for modulos and returns remainder of whole number
print("Which is " + str(dollars) + " dollars and " + str(cents) + " cents.")
choice = input("Do you have more change (y/n)? ")
print("Thanks for using the change calculator.")
finalTotal = TotalVal
print("You had a total of" + finalTotal + " cents.")
print("Which is" + str(finalTotalDollars) + " dollars and" + str(finalTotalCents) + " cents.")

为了在用户想要再次播放时做到这一点,您可以使用外部文件通过open、read和write来存储用户信息。

你可以用记事本作为.txt,写用户名、钱和重复,这样登录就会在名字后面的行检查并调用钱。

最新更新