1 Replies - 1092 Views - Last Post: 27 September 2011 - 11:30 AM Rate Topic: -----

#1 Maximous  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-September 11

Printing a Float and String Error

Posted 27 September 2011 - 11:24 AM

I am working on a program that lets you first select if you wish to convert from Fahrenheit to Celsius or vice versa. I am just starting out and learning this by myself.

# Converts Fahrenheit temperatures to Celsius and Celsius to Fahrenheit

print("Conversions")
# Lists choices
print(" 1. Celsius to Fahrenheit")
print(" 2. Fahrenheit to Celsius")
# Asks the NUMBER of the conversion the user would like to do
choice=input("What would you like to convert (number)? ")
# Detects which conversion the user is asking for
if choice == 1:
    # Inputs the Celsius Temperature
    celsius=float(input("What is the temperatue in Celsius? "))
    # Converts to Fahrenheit
    f =(celsius * 1.8 + 32.0)
    # Display the Fahrenheit
    print("The Temperature is "+f + " Fahrenheit")
    
else:
    # Inputs the Fahrenheit Temperature
    fahrenheit=float(input("What is the temperature in Fahrenheit? "))
    # Converts to Celsius
    c =((fahrenheit - 32)* 0.55)
    # Display the Celsius
    print("The Temperature is "+c+" Celsius")


I have two issues
1. When I run the program it will always goto the second conversion even when 1 is chosen
2. When i try to print the temperature conversion and the variable i get the following error
print("The Temperature is "+c+" Celsius")
TypeError: Can't convert 'float' object to str implicitly
If you need to see it run, I attached the file. I would greatly appreciate any help and advice that can be given, Thank You

Is This A Good Question/Topic? 0
  • +

Replies To: Printing a Float and String Error

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Printing a Float and String Error

Posted 27 September 2011 - 11:30 AM

You're casting input from a string to a float on lines 12 and 20, but on line 8 you'll need to cast to an integer. You need to do this because on line 10 you are checking if it is 1 (the number), not "1" the ASCII string.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1