Jump to content

decimal places in python (NOOB)

ubuntusuperuser
Go to solution Solved by MrSuperb,
inputNumber=float(input("input a number"))

try this.

for a problem question in my digital class i am learning inputs for python like:

 

inputNumber=int(input("input a number"))

 

this works until i input a number with a decimal place like 2.5 or 20.00

i get the error :

inputNumber=int(input("input a number"))

ValueError: invalid literal for int() with base 10: '2.5'
 
how do i input a decimal place number without a error?

PooKloo For Slick

Link to comment
Share on other sites

Link to post
Share on other sites

 

for a problem question in my digital class i am learning inputs for python like:

 

inputNumber=int(input("input a number"))

 

this works until i input a number with a decimal place like 2.5 or 20.00

i get the error :

inputNumber=int(input("input a number"))

ValueError: invalid literal for int() with base 10: '2.5'
 
how do i input a decimal place number without a error?

 

 

An int can only store a whole number, you could use a float instead.

Link to comment
Share on other sites

Link to post
Share on other sites

integer variables arent supposed to be decimals live x,xx.

 

in c++ i would say use "float" :) try it amybe it works

CPU: Intel i7 4790K @4.8GhZ  CPU Cooler: Be Quiet! Dark Rock Pro 2  Motherboard: Gigabyte Z97 UD3H  GPU: Asus ROG RX 480 8G OC Memory: 32GB Gskill Ares 2400Mhz  Storage: 2x Crucial M4 512GB SSD (raid0)  / 1TB Seagate FireCuda SSHD Case: Phanteks Enthoo Evolv ATX PSU: EVGA SuperNOVA P2 750W  Operating System: Windows 10 Enterprise LTSB (64 bit) Other: NZXT Hue+ LED Controller with 8 LED Strips for desk and PC lighting

 

Link to comment
Share on other sites

Link to post
Share on other sites

If you require integers only, then there are two options. You could either round the number to the nearest integer using round(), or reduce the number to the integer before the decimal point using the floor() function.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×