Created
February 24, 2012 01:12
-
-
Save fuzzmz/1896445 to your computer and use it in GitHub Desktop.
Udacity CS101 Homework 1 Ex 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Given a variable, x, that stores | |
#the value of any decimal number, | |
#write Python code that prints out | |
#the nearest whole number to x. | |
#You can assume x is not negative. | |
# x = 3.14159 -> 3 (not 3.0) | |
# x = 27.63 -> 28 (not 28.0) | |
x = 3.14159 | |
#DO NOT USE IMPORT | |
#ENTER CODE BELOW HERE | |
#ANY CODE ABOVE WILL CAUSE | |
#HOMEWORK TO BE GRADED | |
#INCORRECT | |
x += 0.5 # by adding 0.5 we either get the number to "round up" if it's >= than nr.5 or we keep it the same | |
x = str(x) # turn the number into a string | |
print x[:x.find('.')] # print the string up to the period sign |
+= is the same as x = x + something, but yeah, you're right
The challenge remains open :)
This was my solution using only things learned in the unit:
xString = str(x + 0.5)
decimal = xString.find('.')
xString = xString[:decimal]
print xString
That 'decimal' variable could probably just be done in the print statement such as how fuzzmz has done it, but there you go.
goldStars = 2 #IMO
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for posting but that breaks the assignment instructions of "only using concepts covered so far" as we haven't introduced '+=' yet.
Can you find a solution that only uses what has been covered in CS101 Unit 1?