每天加倍的金额,需要将几美分的数量转换为美元+美分

时间:2019-07-01 00:38:28

标签: python

该程序以1美分开始,每天加倍。但是,我坚持尝试寻找一种方法,将便士的数量转换为美元和美分金额。例如,将1020便士转换为$ 10.20。

我也在尝试这样做,因此,如果用户输入的不是正数,则会不断提示用户,直到他们输入正数。但是,这不起作用。

我也觉得自己已经迷惑了使用范围,因为我想输入一个固定的天数,例如16天,当我输入16天时,我会收到1-17天,这是应该做的范围,我不确定该如何解决。

b = int(input("Input number of days "))

if b > 0:
    print(b)
else:
    b = int(input("Days must be positive "))


print("Day 1:","1")
days = 1
aIncrement = 2 
penny = 1



for i in range(b):
    pAmount = int(penny*2) 
    addAmount = int(2**aIncrement -1) 
    aIncrement +=1
    days +=1
    penny *= 2 
    print("Day " + str(days) + ":",pAmount)

2 个答案:

答案 0 :(得分:1)

您的问题包含多个部分,这对于stackoverflow而言并不理想,但是我将尝试全部解决。

修复数值以显示美元和美分。

正如在对其他答案的评论中所指出的那样,由于浮点表示法,除法经常会遇到障碍。但是在这种情况下,由于我们真正关心的是100进入便士计数的次数以及余数,因此我们可以放心使用Python附带的divmod()并计算出一个数字可被另一个数字整除,其余部分可被整数整除。

为清楚起见,divmod()返回一个tuple,在下面的示例中,我解压缩存储在元组中的两个值,并将每个单独的值分配给两个变量之一:dollarscents

    dollars, cents = divmod(pAmount, 100)           # unpack values (ints)
                                                    # from divmod() function
    output = '$' + str(dollars) + '.' + str(cents)  # create a string from
                                                    # each int

解决范围问题

range()函数会产生一个数字,您可以将其设置为所需的开始和结束位置,请记住,结束数字必须设置为比您想要的数字高一个值。即要获取从1到10的数字,必须使用1到11的范围。在代码中,您使用i作为占位符,并分别使用days来跟踪当前日期。由于您的用户将告诉您他们想要b天,因此您需要立即增加该值。我建议将它们结合起来以简化操作,并可能使用稍微更多的自记录变量名。补充说明,因为这是从第一天开始的,所以我们可以删除一些用于在循环开始前手动处理第一天的设置代码(在下一节中有更多介绍)。

days = int(input("Input number of days "))   

for day in range(1, days + 1):
    # anywhere in your code, you can now refer to day
    # and it will be able to tell you the current day

连续输入

如果我们要求用户提供初始输入,则可以输入:

  • 一个负数
  • 一个正数

因此,我们的while循环应检查任何非肯定的条件(即days <= 0)。如果第一个请求为正数,则将有效地完全跳过while循环,并且脚本继续进行,否则它将继续要求其他输入。注意...我在第二个input()函数中编辑了字符串,以向用户显示问题并告诉他们下一步该怎么做。

days = int(input("Input number of days "))

while days <= 0:
    days = int(input("Days must be positive, input positive number of days: "))     

将所有这些放在一起,代码可能看起来像这样:

我将上面的物品放在一起,并清理了一些其他东西。

days = int(input("Input number of days "))

while days <= 0:
    days = int(input("Days must be positive, input number of days: "))

# aIncrement = 2        # this line not needed
penny = 1

for day in range(1, days + 1):
    pAmount = int(penny)                      # this line was cleaned up
                                              # because we don't need to manually
                                              # handle day one
    dollars, cents = divmod(pAmount, 100)
    output = '$' + str(dollars) + '.' + str(cents)
    # addAmount = int(2**aIncrement -1)    # this line not needed
    # aIncrement +=1                       # this line not needed
    penny *= 2 
    print("Day " + str(day) + ":", output)

答案 1 :(得分:0)

对于连续提示,您可以使用while循环。

while True:
    user_input = int(input("Enter the number"))
    if user_input > 0:
        break
    else:
        continue

或者:

user_input = int(input("Enter the number"))

while user_input <= 0:
    user_input = int(input("Enter the number"))

对于范围问题,可以在传递范围的参数中加上-1。

for i in range(b - 1):
相关问题