-1

I'm stuck on a beginner level coding problem. Goal is to round a float number and output an integer. So x = 3.14159, output 3 (not 3.0). We're supposed to be able to do this using only what we've learned, and what we've learned is only 3 functions: .find, <string>[:], and converting the given x = float num into a string, via str().

How should I be thinking about this? When I write out what logically needs to happen, I always find myself needing if().

I ended up getting partial credit with the following code: given: x = 3.14159

x = int(round(x))
print x

But I would like to solve it without int() or round(), or if(). My first thought was to use x[2:3] and x[3:4] to check the tenths and hundredths place values, but to proceed I still run into the if() wall.

Ouroborus
  • 16,237
  • 4
  • 39
  • 62
I3C
  • 3
  • 2
  • Generally speaking, Convert to string, find the `'.'` character, and slice to that point. If you want more specific help, please [edit] to include a [mcve] showing the code that you've tried so far so that we can give specific answers – G. Anderson Jan 26 '23 at 20:42
  • 4
    So you're taking a class in How Not To Use Python, basically? You're being asked to do something that isn't actually a mathematical operation at all. – jasonharper Jan 26 '23 at 20:47
  • While this question doesn't encourage you to take advantage of Python predefined tools, one answer seems to me is to to use a mathematical solution. Another one like others have mentioned to convert to a string. `x = str(x)` then `x.split('.')` which would give you a list of two parts the whole part and the decimal/fractional part. – GIZ Jan 26 '23 at 20:56
  • Since you are rounding, first add 0.5 to the value before converting to string. Then just take the characters to the left of the decimal point. – RufusVS Jan 26 '23 at 20:59
  • 1
    `if` is not a function btw, you don't need to add parentheses, a way (never actually do this but if it's allowed here, sure) to emulate `if` statement is via using a `while` loop, what you essentially do is the same as with an `if` so like `while condition:` and then at the end you just break out of it immediately meaning it runs exactly once and only if the condition is met – Matiiss Jan 26 '23 at 21:04
  • I suspect that you're supposed to convert the number to a string, find the period, and then only print the part before that. – chw21 Jan 26 '23 at 20:41
  • 2
    Do you mean to **round** the number or **truncate** it? What would you do with 2.718? – Adrian Mole Jan 26 '23 at 21:23
  • 1
    Rather than using slicing to try to look at parts of the string and *check* them (which means, you know, `if`), can you think of a way to just take a part of the string **that necessarily** represents an integer? (Hint: can you think of a symbol that's in the string, that can't possibly be in the representation of an integer?) – Karl Knechtel Jan 26 '23 at 21:23
  • 1
    Has your teacher taught you about loops yet? If not, I do hope they mean truncation rather than rounding. Actually rounding using _only_ those tools is not straightforward for someone new to programming, and requires loops and some tricky code. – Ryan M Jan 26 '23 at 21:30
  • Okay, I went and wrote out most of a solution for rounding non-negative numbers less than 9 without anything other than if/else, for, find, str, and slices, and even that's already trickier than anything I'd give to someone at this stage of learning (I'm not going to link it because honestly it's bad advice; you shouldn't use the programming techniques in it). I'm pretty sure you were supposed to truncate: that is, just trim off everything after the decimal. – Ryan M Jan 26 '23 at 21:52
  • @RyanM idk, seems like they weren't even allowed to use `if` – Matiiss Jan 26 '23 at 21:55

2 Answers2

0

You should use the built in string format feature:

x = 123.35643
rounded = f'{x:.2f}'
print(rounded)

Your output would be: 123.36

Or to get the whole number:

rounded = f'{x:.0f}'
print(rounded)
eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20
  • 1
    I'd think that's not something they've learnt yet and the way they are supposed to approach this seems rather clear to me, but sure, this works for a general purpose I guess – Matiiss Jan 26 '23 at 21:01
  • Well he mentioned having to turn it into a string. This is the only real way to turn it into a string and round in one line. – eatmeimadanish Jan 26 '23 at 21:24
0

This is a poorly constructed string manipulation problem because you'd never actually go about solving this problem this way. But using only what you've been taught, you can accomplish the task:

x = 3.14159

# 0.5 (optional?) add 0.5 to x so that it will be properly rounded
#     when removing the digits after the decimal
x += 0.5

# 1. convert the int to a string
x_str = str(x)

# 2. find the index of the decimal point in the string
decimal_idx = x_str.find('.')

# 3. slice the string from the beginning to the decimal
x_int_str = x_str[:decimal_idx]

# 4. do something with the value
print(x_int_str)
Woodford
  • 3,746
  • 1
  • 15
  • 29
  • 1
    @Matiiss Truncation is one type of rounding. There is more than one valid strategy to perform rounding. See: https://stackoverflow.com/questions/14249971/why-is-pythons-round-so-strange – Woodford Jan 26 '23 at 21:18
  • Incrementing x by 0.5 is not optional. I didn't know that you could add 0.5 to any float number, ignore right of the decimal, and have a rounded integer. So simple! – I3C Jan 28 '23 at 06:59