4

I'm making a Temperature Calculator Celsius and Farenheit

I'm a completely begginer in Python

I have Python 3.3

So, I made this function to calculate a Farenheit value to a Celsius value

def C():
 print('Enter a temperature:')
 Fvalue = input()
 print(int(Fvalue) - int(32) * int((5/9)))

I run it and it just prints the Fvalue itself, it doesn't make the math operations

I hope you can help me guys

Ashwini Chaudhary
252k60 gold badges479 silver badges520 bronze badges
asked Jul 3, 2013 at 18:24
2
  • I fixed the indentation, assuming that was not the source of your problem. Commented Jul 3, 2013 at 18:27
  • You are doing integer arithmetic. This means that int(5/9) is 0. Commented Jul 3, 2013 at 18:30

4 Answers 4

2

Your real problem lies here:

int(5/9)

5/9 gives you 0.555 which when cast to int() gives you 0

Try this:

print((int(Fvalue) - 32) * 5/9)
answered Jul 3, 2013 at 18:25
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! in some minutes I give you accepted answer
2

The problem is, you are casting the value of 5 / 9 to an int, which will give you 0 as a value. Just remove the cast, and it will be fine. And also you need to add parenthesis around the subtraction.

Change your print statement to:

print((int(Fvalue) - 32) * 5/9)
answered Jul 3, 2013 at 18:25

5 Comments

Or, just use a constant of 1.8? :)
Yeah, I think it's the int(5/9) that's the problem.
@AshwiniChaudhary. Ok, so typecasting seems to be problem. Will update answer.
@AshwiniChaudhary Except the OP then converts the result of division to an int.
@FredLarson With the suitably reversed op of course.... (Fvalue - 32) / 1.8 or CValue * 1.8 + 32 - saves the whole stupid 5 and 9 stuff...
0

int((5/9)) gives you 0, so int(32) * int((5/9)) is 0, so you simply print Fvalue

This should fix your issue:

print((int(Fvalue) - 32) * 5.0 / 9))

With some easy math, this expression would be cleaner:

print (Fvalue - 32) / 1.8
answered Jul 3, 2013 at 18:27

Comments

0

Your code be should be as given below, u don't require casting for integers 32 and (5/9),

def C():
print('Enter a temperature:')
Fvalue = input()
print(int(Fvalue) - 32 * 5/9)
answered Nov 9, 2017 at 8:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.