【问题标题】:Round to whole numbers without using conditional statements in Python - Logic在 Python 中不使用条件语句四舍五入到整数 - 逻辑
【发布时间】:2015-02-08 23:19:25
【问题描述】:

我正在 Udacity 学习 Python 课程,我正在尝试自己解决这个问题,而不看答案。或许你可以给我一个逻辑提示?

以下是说明以及我目前掌握的内容。我们还没有学习条件语句,所以我不能使用它们。我们只学习了如何分配/打印变量、字符串、索引字符串、子序列和 .find。他们刚刚在最后一个练习中介绍了 str 命令。

# Given a variable, x, that stores the 
# value of any decimal number, write Python 
# code that prints out the nearest whole 
# number to x.
# If x is exactly half way between two 
# whole numbers, round up, so
# 3.5 rounds to 4 and 2.5 rounds to 3.
# You may assume x is not negative.

# Hint: The str function can convert any number into a string.
# eg str(89) converts the number 89 to the string '89'

# Along with the str function, this problem can be solved 
# using just the information introduced in unit 1.

# x = 3.14159 
# >>> 3 (not 3.0)
# x = 27.63 
# >>> 28 (not 28.0)
# x = 3.5 
# >>> 4 (not 4.0)

x = 3.54159

#ENTER CODE BELOW HERE

x = str(x)
dec = x.find('.')
tenth = dec + 1

print x[0:dec]

////

所以这让我将字符打印到小数点,但我不知道如何让计算机检查“第十”是 > 4 还是 并且打印出来根据答案的东西。

我想如果“第十个”不是 > 4,我可能会走得足够远让它返回 -1,但我不知道如何让它打印 x[0:dec] 如果 它是 4。

:/

有人可以在正确的方向上轻推我吗?

【问题讨论】:

    标签: python logic rounding


    【解决方案1】:

    这是一个奇怪的限制,但你可以这样做:

    x = str(x)
    dec_index = x.find('.')
    tenth_index = dec_index + 1
    tenth_place = x[tenth_index] # will be a string of length 1
    should_round_up = 5 + tenth_place.find('5') + tenth_place.find('6') + tenth_place.find('7') + tenth_place.find('8') + tenth_place.find('9')
    
    print int(x[0:dec_index]) + should_round_up
    

    我们要做的是查看十分位。由于.find() 在未找到参数时返回 -1,因此如果十分位为 5、6、7、8 或 9,.find() 调用的总和将为 -4(因为 @987654324 之一@ 调用将成功并返回 0),但如果十分位为 0、1、2、3 或 4,则为 -5。我们将其加上 5,因此如果我们应该四舍五入,should_round_up 等于 1 , 否则为 0。将其添加到整数部分,我们就完成了。


    也就是说,如果你不受这种人为限制,你会这样做:

    print round(x)
    

    然后继续你的生活。

    【讨论】:

    • 啊!我认为它是这样的,但我仍然不知道如何执行它。同意。奇怪的限制。我猜他们试图让我们使用某种逻辑方法。
    • 虽然,我刚刚意识到,我们还没有学习 int。这么奇怪。非常感谢你的帮助。我会看看他们的答案是什么,然后在这里发布。
    • 天啊。太简单了! o.O 当我们打印到小数点时,我们必须在数字上加上 0.5。 x = 3.14159 #ENTER CODE BELOW HERE x = x + 0.5 x = str(x) dec = x.find('.') print x[:dec]
    • 哦。是的,这显然是比这个更好的方法。
    【解决方案2】:

    从接受的答案来看,您只期望浮点数,因此解决起来非常简单:

    x = 3.54159  
    # split on .
    a, b = str(x).split(".")
    # cast left side to int and add result of test for right side being greater or equal to 5
    print(int(a) + (int(b) >= 5))
    

    (int(b) > 5) 将是 1 或 0,即True/False,所以我们要么在右侧 > .5 时加 1,要么在

    如果你是在数学上做的,你只需要print(int(x+.5)),任何 >= .5 都意味着 x 在

    【讨论】:

    • OP 明确表示他不应该使用条件语句。
    • 正是...如果我们可以使用 if/else 语句,但这是一个限制。感谢您的帮助。我期待 if/else!
    【解决方案3】:
    x = 3.54159  
    # split on .
    a, b = str(x).split(".")
    # cast left side to int and add result of test for right side being greater or equal to 5
    print(int(a) + (int(b[0]) >= 5))
    
    # above code will not work with 3.14567 and the number with having two or more digits after decimal
    

    【讨论】:

    • 以上代码不适用于 3.14567 和小数点后两位或以上的数字
    • 欢迎来到 Stack Overflow!这是对this answer 的评论。请不要发布 cmets 作为答案。在这里获得 50 分后,您就可以发布 cmets。
    【解决方案4】:

    我认为这更容易......

    x = x + 0.5
    intPart, decPart = str(x).split(".")
    print intPart
    

    例子:

    • 如果 x = 1,则它将变为 1.5,intPart 将为 1。
    • 如果 x = 1.1,则它将变为 1.6,而 intPart 将为 1。
    • 如果 x = 1.6,则它将变为 2.1,而 intPart 将为 2。

    注意:它只适用于正数。

    【讨论】:

    • 聪明的解决方案,如果可能的话,将其扩展为也适用于负数:)
    【解决方案5】:

    此代码会将数字四舍五入到最接近的整数

    不使用条件

    你可以这样做

    x = 3.54159
    
    x = x + 0.5       # This automatically takes care of the rounding
    
    str_x = str(x)    # Converting number x to string 
    
    dp = str_x.find('.')    # Finding decimal point index
    
    print str_x[:dp]        # Printing upto but excluding decimal point
    

    【讨论】:

    • 如果您愿意,可以使用int(str_x[:dp])将字符串转换回整数以供将来计算
    【解决方案6】:

    我在 Udacity 上过同样的课程。使用以下代码解决了它:

    y = str(x)
    decimal = y.find('.')
    y_increment = y[decimal+1:]
    print decimal
    print y_increment
    
    # Section below finds >5
    check5 = y_increment.find('5',0,1)
    check6 = y_increment.find('6',0,1)
    check7 = y_increment.find('7',0,1)
    check8 = y_increment.find('8',0,1)
    check9 = y_increment.find('9',0,1)
    yes_increment = (check5 + 1) + (check6 + 1) + (check7 + 1) + (check8 + 1) + (check9 + 1)
    print check5, check6, check7, check8, check9
    
    #Calculate rounding up
    z = x + (yes_increment)
    
    z = str(z)
    final_decimal = z.find('.')
    print z[:final_decimal]
    

    【讨论】:

      猜你喜欢
      • 2021-08-05
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多