Results 1 to 6 of 6

Thread: How to reject decimal numbers in Python?

  1. #1
    Join Date
    Aug 2006
    Posts
    201

    How to reject decimal numbers in Python?

    I have searched a lot but not able to find the appropriate answer. So lastly hoping some help from you guys.!! I just want to ask that Is there a way to stop users inputting decimal numbers in python? I am having the following code :
    user = input("Please input a number")
    when this is popped to user I want that user should not enter the decimal numbers. They should able to enter only the whole numbers. Please help me soon.!! Also does anyone know the program to convert Decimal to Binary??
    Last edited by The Recruiter; 15-01-2010 at 05:22 PM.
    Don't be afraid of darkness, it's in your heart anyway.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to reject decimal numbers in Python?

    According to me you can use the 'If statement' to say if its a decimal print a message to say whole numbers only. That means when the user would input the decimal numbers then there would be a message generated to the user to enter the Whole numbers only. You have to put that into the loop, and it would be somewhat like this :
    Loop -> Input -> Is Input Integer?
    -(yes)-> break loop
    -(no)-> go through loop again
    Hope that you got the solution that you needed.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: How to reject decimal numbers in Python?

    I would like to give an another example. Lets say your input is a floating point number x. The number will get rounded from z to n decimal places, if you use the built in function round(z,n) in python. If you omit the n, then it assumes it as 0. So if the user inputs the x which is an integer, then x will be equal to round(x). If x is not an integer, then x will not be equal to round(x).
    For Example :

    x = 3
    round(x) = 3
    x == round(x)

    x = 5.2
    round(x) = 5
    x != round(x)

    I think that from this you should able to write the code correctly.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to reject decimal numbers in Python?

    I think that you can use the following code :

    number = raw_input('enter something: ')

    if '.' in number:
    print "NO DECIMALS ALLOWED"

    else:
    print number

    using this program to can get the desired output.

  5. #5
    Join Date
    May 2008
    Posts
    2,012

    Re: Code for conversion of Decimal to Binary

    I think that this code will work for converting the Decimal to Binary :

    def Denary2Binary(n):
    '''convert denary integer n to binary string bStr'''
    bStr = ''
    if n < 0: raise ValueError, "must be a positive integer"
    if n == 0: return '0'

    while n > 0:

    bStr = str(n % 2) + bStr
    n = n >> 1
    return bStr
    def int2bin(n, count=24):
    """returns the binary of integer n, using count number of digits"""
    return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)])

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    Re: How to reject decimal numbers in Python?

    I think that code mentioned by the 'Katty' has disadvantage. The coding will gets executed when used as a standalone program, but not as an imported module. If you save this module as trialpro.py and and use it in another program. Now when you try to import the trialpro.py__name__namespace would now be a trialpro.py and the test will be ignored. Hope that I will find some way for this drawback.

Similar Threads

  1. How to change negative numbers to positive numbers in Excel
    By Shaina Na in forum Microsoft Project
    Replies: 3
    Last Post: 08-01-2012, 05:35 PM
  2. Need C program code to count numbers of negative & positive numbers
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 02:00 PM
  3. How to display decimal numbers in Excel sheet
    By Halina in forum Windows Software
    Replies: 3
    Last Post: 05-05-2009, 03:13 PM
  4. Download Python 3.0 / Python 3000
    By Amaresh in forum Software Development
    Replies: 6
    Last Post: 24-02-2009, 09:28 AM
  5. SQL Query for Searching Missing Numbers from Sequence of Numbers
    By Bhagwandas in forum Software Development
    Replies: 3
    Last Post: 18-02-2009, 01:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,311,732.81856 seconds with 17 queries