## INTEGER ISSUE

This is the place for queries that don't fit in any of the other categories.

### INTEGER ISSUE

Soo... when I'm doing if statements this is how I have to do them or it doesn't work:
if int(test)==1:
But what if test is a decimal? Then I get an error. How do I fix this?
WITCHKING

Posts: 5
Joined: Thu Mar 14, 2013 4:15 am

### Re: INTEGER ISSUE

Soo... when I'm doing if statements this is how I have to do them or it doesn't work:
if int(test)==1:
But what if test is a decimal? Then I get an error. How do I fix this?

have you ran that code in a python interpreter?
What error do you get?
What have you tried to fix this error?
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam

metulburr

Posts: 2145
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: INTEGER ISSUE

Code: Select all
`if round(test) == 1:    # ...`

If test is a string, do a float() conversion before rounding.
setrofim

Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

### Re: INTEGER ISSUE

Is there anyway I could do an if statement without using int?
WITCHKING

Posts: 5
Joined: Thu Mar 14, 2013 4:15 am

### Re: INTEGER ISSUE

What do you mean? What are you actually trying to achieve? As metulburr said, post some code.
setrofim

Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

### Re: INTEGER ISSUE

Do you mean that the value 1 could be say between 0.5 & 1.5
if so you could do something like this if 0.5 <= test <= 1.5:
Code: Select all
`test = 1.9print True if 0.5 <= test <= 1.5 else Falsetest = 1.2print True if 0.5 <= test <= 1.5 else Falsetest = 0.49print True if 0.5 <= test <= 1.5 else FalseFalseTrueFalse`
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1617
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: INTEGER ISSUE

setrofim wrote:What do you mean? What are you actually trying to achieve? As metulburr said, post some code.

What I'm trying to achieve doesn't really matter, but here's an example. Let's say I'm trying to make a machine that tests the number you inputted to see if it is 1 or 0.
one=input("Type either 1 or 0 here:")
if int(one)==0:
....
if int(one)==1:
....
So, how do I do the if statements WITHOUT using the int command.
WITCHKING

Posts: 5
Joined: Thu Mar 14, 2013 4:15 am

### Re: INTEGER ISSUE

what version of python are you using?
Snaek

Posts: 6
Joined: Thu Mar 14, 2013 1:15 am

### Re: INTEGER ISSUE

input (on python 3), and raw_input (on python 2) return strings. That is just what they do. Once you get your string you have to convert it. You can of course immediately convert the return of your input as follows (on python 3):
Code: Select all
`one=int(input("Type either 1 or 0 here:"))`
but either way you will have to add some code so your program doesn't crash if your user enters something that isn't a number.

input on python 2 did what you are expecting but it was and is a horrible idea so it was removed in python 3 and no one who still codes in python 2 uses it.

And btw:
What I'm trying to achieve doesn't really matter
This is untrue. You are the only one who knows what you expect your code to do. We can't know from incorrectly written code or ill conceived ideas what you are expecting it to do.

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1687
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: INTEGER ISSUE

OK: So here is what I need to do.
All I need to do is this, have the user set a variable, than test the variables value, and divide it by 10. All I need is to be able to have the user input a decimal number, and make it still work. Get it?
WITCHKING

Posts: 5
Joined: Thu Mar 14, 2013 4:15 am

### Re: INTEGER ISSUE

WITCHKING wrote:OK: So here is what I need to do.
All I need to do is this, have the user set a variable, than test the variables value, and divide it by 10. All I need is to be able to have the user input a decimal number, and make it still work. Get it?

Its you that doesn't get it, you've been told already that the bottom line is input returns a string, if you want the returned value to be something other then a string you must convert it.
if you want an input function that returns a decimal, make your own function that uses input.
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1617
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: INTEGER ISSUE

Soo... I'm a python newbie. I'm also a programming newbie... I just started, so how do I make a function that would do that?
WITCHKING

Posts: 5
Joined: Thu Mar 14, 2013 4:15 am

### Re: INTEGER ISSUE

I use python 2.7, i think your on python 3 but you never really said so i think the following is python 3 code.
Code: Select all
`def input_int(prompt):    '''Read a string from standard input and return an int when valid'''    while True:        try:            return int(float(input(prompt)))        except ValueError:            print('Please enter a numeric value only')`
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1617
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: INTEGER ISSUE

If you want it to maintain decimals use float, if you want it to truncate decimals but still allow them, use int. Both int and float will work on strings that appear to be numeric.

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1687
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona