INTEGER ISSUE

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

INTEGER ISSUE

Postby WITCHKING » Thu Mar 14, 2013 4:16 am

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

Postby metulburr » Thu Mar 14, 2013 4:51 am

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?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1388
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: INTEGER ISSUE

Postby setrofim » Thu Mar 14, 2013 6:42 am

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

Postby WITCHKING » Thu Mar 14, 2013 10:31 pm

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

Postby setrofim » Thu Mar 14, 2013 11:13 pm

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

Postby Yoriz » Thu Mar 14, 2013 11:49 pm

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.9
print True if 0.5 <= test <= 1.5 else False
test = 1.2
print True if 0.5 <= test <= 1.5 else False
test = 0.49
print True if 0.5 <= test <= 1.5 else False

False
True
False
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: INTEGER ISSUE

Postby WITCHKING » Sat Mar 16, 2013 3:33 am

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

Postby Snaek » Sat Mar 16, 2013 3:54 am

what version of python are you using?
Image
Snaek
 
Posts: 6
Joined: Thu Mar 14, 2013 1:15 am

Re: INTEGER ISSUE

Postby Mekire » Sat Mar 16, 2013 4:21 am

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
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: INTEGER ISSUE

Postby WITCHKING » Sat Mar 16, 2013 5:21 pm

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

Postby Yoriz » Sat Mar 16, 2013 5:36 pm

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.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: INTEGER ISSUE

Postby WITCHKING » Sat Mar 16, 2013 6:49 pm

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

Postby Yoriz » Sat Mar 16, 2013 8:15 pm

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')
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: INTEGER ISSUE

Postby Mekire » Sun Mar 17, 2013 1:33 am

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
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider], snippsat and 3 guests