在 Python 中不使用条件语句四舍五入为整数 - 逻辑

Round to whole numbers without using conditional statements in Python - Logic

我正在 Udacity 上 Python 课程,我正在尝试自己解决这个问题而不看答案。也许你能给我一些逻辑提示?

以下是说明以及我目前掌握的内容。我们还没有学习条件语句,所以我不能使用它们。我们只学习了如何 assign/print 变量、字符串、索引字符串、子序列和 .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]

////

所以这让我打印了小数点之前的字符,但我不知道如何让计算机检查 "tenth" 是否 > 4 或 < 5 and 根据答案打印出一些东西。

我想如果 "tenth" 不是 > 4,我可能会得到 return -1,但我不知道如何让它打印 x [0:dec] if 小于 5,x[0:dec]+1 大于 4。

:/

有人可以给我一个正确方向的提示吗?

根据已接受的答案判断,您只需要浮点数,因此解决起来非常简单:

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,要么在它 < .5 时加底并加 0。

如果您在数学上这样做,您只需要 print(int(x+.5)),任何 >= .5 都意味着 x 将被四舍五入并在 < .5 时取整。

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

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() returns -1 如果未找到参数,如果第十位是 5、6、7、8,则 .find() 调用的总和将为 -4,或 9(因为 .find() 调用之一会成功并且 return 0),但如果第十位是 0、1、2、3 或 4,则为 -5。我们再加上 5,这样 should_round_up 如果我们应该四舍五入则等于 1,否则等于 0。将其添加到整数部分,我们就完成了。


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

print round(x)

然后继续你的生活。

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

我在 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]

我觉得这样更容易...

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。

注意:它只适用于正数。

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

不使用条件

你可以这样做

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