2014/04/25 (Fri) 19:26

Rounding Numbers On Python

これこんなんでいいのか?


# 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 = 9.89327

#ENTER CODE BELOW HERE

decimal = str(x).find('.')
num1 = str(x)[decimal + 1]
rounded1 = x + 1 + num1.find("5")
rounded2 = rounded1 + 1 + num1.find("6")
rounded3 = rounded2 + 1 + num1.find("7")
rounded4 = rounded3 + 1 + num1.find("8")
rounded5 = rounded4 + 1 + num1.find("9")
decimal = str(rounded5).find('.')
print str(rounded5)[:decimal]


Correct! 
関連記事
trackback*0 | comment*1 | 未分類
comment
名無しのブラゲ民 #-
だんご生きてたんですね
モゲラで何かのCGI改造したカービィ限定のゲームみたいにしたのありましたよ
2014/04/29(火) 00:01:44 | URL | edit
URL:
Comment:
Pass:
Secret: 管理者にだけ表示を許可する
 
trackback
この記事のトラックバックURL
この記事へのトラックバック
profil