divide a decimal

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

divide a decimal

Postby coreyf1000 » Sun May 05, 2013 6:00 pm

I need to divide a fraction by a whole number but I don't see what I did wrong and I have been fighting with this little program for longer then I care to admit

Code: Select all
a = int(input("enter: "))
b = int(input("enter: "))
print(a / b)
input("\n\npress the enter key to exit")


if I make a = 1
and b = 2
I get a/b = .5
it runs no problem

but if I do a = .5 python blows up and reads

Code: Select all
Traceback (most recent call last):
  File "C:/Users/xxxxxx/Desktop/python3/easy.py", line 1, in <module>
    a = int(input("enter: "))
ValueError: invalid literal for int() with base 10: '.5'



I have been reading, search and watching youtube videos but I cant get it to take a decimal in puts

I have tried strings and just cant get it

fyi new member first post

thanks for any help
coreyf1000
 
Posts: 4
Joined: Sun May 05, 2013 1:15 pm

Re: divide a decimal

Postby Yoriz » Sun May 05, 2013 6:16 pm

I find if you turn the string into a float before turning it to an int it works ok.
Code: Select all
    a = int(float(input("enter: ")))
    b = int(float(input("enter: ")))
    print(a / b)
    input("\n\npress the enter key to exit")
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1170
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: divide a decimal

Postby coreyf1000 » Mon May 06, 2013 2:49 am

thank you for the reply using the float I think I will get it working but not quite there on my end
coreyf1000
 
Posts: 4
Joined: Sun May 05, 2013 1:15 pm

Re: divide a decimal

Postby coreyf1000 » Mon May 06, 2013 2:50 am

ok a little tweaking and I have the math working but the out put is in scientific notation

Code: Select all
a = float(input("enter: "))
b = float(input("enter: "))

c = a / b
print(c)
input("\n\npress the enter key to exit")


Code: Select all
enter: .594
enter: 74430
7.98065296252e-06


press the enter key to exit


any thoughts on how to get the out to be in decimal form
fyi this is some of the numbers I need to work with

running python 3.1.1
coreyf1000
 
Posts: 4
Joined: Sun May 05, 2013 1:15 pm

Re: divide a decimal

Postby Mekire » Mon May 06, 2013 3:27 am

What would you like it to look like?

Code: Select all
a = float(input("enter: "))
b = float(input("enter: "))

c = a / b
print("{:.7f}".format(c))
input("\n\npress the enter key to exit")
Code: Select all
>>>
enter: .594
enter: 74430
0.0000080

-Mek
User avatar
Mekire
 
Posts: 1125
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: divide a decimal

Postby coreyf1000 » Mon May 06, 2013 3:38 am

Code: Select all
a = float(input("enter: "))
b = float(input("enter: "))

c = a / b
print("{:.8f}".format(c))
input("\n\npress the enter key to exit")


this fixed it I changed the decimal place to 8 from 7 and got 0.00000798 jut what I needed thanks
coreyf1000
 
Posts: 4
Joined: Sun May 05, 2013 1:15 pm


Return to General Coding Help

Who is online

Users browsing this forum: mdovey and 3 guests