EX 15 - I'm failing miserably to make the second gets into an integer

I copied the instructions by hand. Here it is:
image
indent preformatted text by 4 spaces
print "give me a number: "
number = gets.chomp.to_i

bigger = number * 100
puts “A bigger number is #{bigger}.”

print "Give me a another number: "
another = gets.chomp
number = another.to_i

print number
smaller = number / 100
puts “A smaller number is #{smaller}.”

idk how to make it look all formatted properly and am unsure whether the pic I tried to include will work. Anyways, I googled, printed the variable, it’s returning properly, but the result is always 0. I removed .to_i after “another” where defining “number” thinking the above to_i was canceling it out, that failed. Sorry to bother but does anyone know whats wrong? I hypothesize that this is karmic justice because her tarrot reader said she was my punishment for something I did in a past life. lol.

OK, so I copied the subject text into a new file and got the same result. Capture It works until the very end…

I think if you change your 100 to 100.0 on the smaller calculation it will work how you think.

1 Like

I appreciate the quick reply. It sadly didn’t fix the problem

Looks like your code is the same though. You have this:

smaller = number / 100

But it needs to be:

smaller = number / 100.0

That tells python to make the number 100 into a floating point (decimal) number because you added .0 at the end. Then the math will convert everything to float and should give you close to what you want.

But, you should also write out what you expect it should print vs. what it’s currently printing.

1 Like

It worked. But I wish I understood why I needed to do that with Ruby. Like, nobody else has that problem? I ran the program on Atom and Gedit, no luck absent that “.0” after 100.

It’s because 8 / 100 is considerable less than 1. So if you round this an an integer it is 0.

It’s recommended to use floats for division or weird stuff happens. Like 3 / 2 as int.

2 Likes

When you say “I ran the program on Atom and Gedit”… do you mean you hit some button in the text editor like F5 and ran it, rather than using Terminal to run it?

You really need to run these in terminal. Especially if running in Atom can’t even get the floating point calculation right.

Haha no, sir, I’m doing as instructed! Promise! I thought maybe Atom was glitching and I had been using gedit before starting this routine so I popped back over to rewrite the code there under a different filename and proceeded to execute from cmd. Ignore the extra prints, they are from me trying to isolate when the variable broke down to 0. Thank you Kesley, that tracks for me to follow. Thanks to you both!

Great, just checking. That trips up a lot of people. Also, you can post code here as text. Just do this:

```
Your code here
```

That’s 3 backticks, and also works inside a sentence like this which is useful when you want to talk about __init__ methods.

You can also do it this way:

[code]
# your code here
[/code]

If you can’t use the backticks.

1 Like
A free service run by Zed A. Shaw for learncodethehardway.org.