Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Python - Whole integers only


  • Please log in to reply

#1
rev_olie

rev_olie

    Member

  • Member
  • PipPipPip
  • 532 posts
Hey :) ,

Just a quick answer after some pretty hefty research on the topic - Is there a way in python to stop users inputting decimal numbers?

I.e if i had the following:

user = input("Please input a number")

how could i stop them doing...
user = 0.5

I know theres the int(x) function but i don't think that would stop the user. I was thinking maybe an If statment to say if its a decimal print a message to say whole numbers only...
  • 0

Advertisements


#2
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts

I was thinking maybe an If statment to say if its a decimal print a message to say whole numbers only...

That would work just fine :)
  • 0

#3
rev_olie

rev_olie

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 532 posts

I was thinking maybe an If statment to say if its a decimal print a message to say whole numbers only...

That would work just fine :)


Any idea how?
I can do the IF part but how do I block the actual decimal number?
  • 0

#4
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
Put it into a loop, so you have something like:
Loop -> Input -> Is Input Integer? 
								-(yes)-> break loop
								-(no)-> go through loop again
That way, if the user inputs a decimal number, you can write an appropriate error message then ask for the input again.
  • 0

#5
rev_olie

rev_olie

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 532 posts
Ah ok makes sense, but how do I actually define the number as being a decimal?
  • 0

#6
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
Not sure I understand what you mean. I'm not very familiar with Python at all. Why do you have to define the number as a decimal?
  • 0

#7
rev_olie

rev_olie

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 532 posts
I mean more in the way of what to set the if statement at.
For example in python if I was to stop a number say greater than 5 I could write:

number = input("Enter a number ")
if number >5:
 print "That number is greater than 5"
else:
 print "that number is less than 5"

So my poorly written, may i add :) question last time meant what do I put after the IF to tell it to stop a decimal number. I.e in the example above what would I swap the >5 for to stop decimal numbers?
That's what I'm stuck on at the moment :) is basically how to actually tell it to stop decimal numbers... :)
  • 0

#8
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
OK, how about this. Lets say your input is a floating point number x. There is a built in function round(z,n) in python which will round number z to n decimal places. If n is omitted, then it assumes 0.

Therefore, if x 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)
Understand what I'm getting at? From that you should be able to write your code correctly :)

Keep in mind that since the function round works on floating point numbers, you will have to convert the raw input to floating point.
  • 0

#9
rev_olie

rev_olie

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 532 posts
Ah ok yeah I can see where that is going. So that takes the decimal you input and rounds it up?

If it rounds it is there another way? My issues is that the user is going to input a score and then the program will automatically assign it a letter.
For example the programs result will be F if the number input is in-between 35 - 40 or it will display say a G if the number is in-between 41-45.

So by rounding it up I am going to be changing the score and that won't be allowed. The program really needs to just stop the decimal all together.
Sorry about that :) , but thanks for the suggestion :).
  • 0

#10
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
Well you only want the score if it's an integer, correct? So applying round to an integer will leave it unchanged.
  • 0

#11
rev_olie

rev_olie

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 532 posts
mpascal,

Thank you very much for your time :) , I can definatley say I've learned something. I have implemented the code structure you gave and it worked like a charm.

I also finally found another alternative to, I found a python genius and gave me another alternative:
number = raw_input('enter something: ')

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

else:
   print number

Basically stopping the decimal point, and you can have a decimal without a point...well at lest I don't think you can.

Anyhow thought I would share that and thanks again :)
  • 0

#12
mpascal

mpascal

    Math Nerd

  • Retired Staff
  • 3,644 posts
Well it's certainly possible (for example, fractions or negative exponents), but I doubt anyone would go to lengths such as those to create an error.

Thanks for sharing that. As you can see, there is always more than one solution to a problem :)
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP