此程序将要求用户提供其拥有的半美元、25美分、10美分、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.")
为了在用户想要再次播放时实现这一点,您可以使用“打开”、“读取”和“写入”来存储用户信息,使用外部文件进行写入和读取
您可以使用notepad as.txt并写入用户名、货币和重复,因此登录时会在名称后的行中检查并调用货币
相关问题 更多 >
编程相关推荐