English subtitles

← Rounding Numbers - Intro to Computer Science

Get Embed Code
7 Languages

Showing Revision 6 created 05/24/2016 by Udacity Robot.

  1. [Male] For the last question in Homework 1,
  2. you're going to solve the problem of how to round numbers,
  3. and this is going to require using a new Python function we haven't
  4. introduced yet in lecture, but you'll be able to figure it out
  5. as part of this homework problem.
  6. But it's a pretty tough question, so that's why it has 2 gold stars.
  7. The new function that you'll use is the str function.
  8. The str function takes as input any number,
  9. and it turns that number into a string, so the output of str
  10. applied to a number is a string that represents the number that was passed in,
  11. so if we evaluated str on the number 89,
  12. what that evaluates to is the string containing the characters 8 and 9.
  13. So it's a string that looks like the number that was passed in,
  14. but instead of being a number, now it's a string.
  15. If you understand str, you'll be able to solve this question.
  16. Your goal for this question is given a variable x,
  17. so at the beginning of your code, x has been initialized to store the value
  18. of any decimal number.
  19. What your code should do is print out the nearest whole number to x.
  20. So here's a few examples.
  21. If the value of x is 3.14159,
  22. what your code should print out is 3.
  23. If the value of x is 27.63,
  24. your code should print out 28.
  25. And to keep this a little simpler, you can assume that the value of x
  26. is not negative.
  27. This is going to require you to combine several things
  28. that you've seen in Unit 1.
  29. The only new thing that you need to solve this is the str function
  30. that we've just explained.
  31. Everything else that you need to solve this is something that you already know
  32. from Unit 1.