2 Replies - 1575 Views - Last Post: 06 February 2012 - 01:20 AM Rate Topic: -----

#1 bk53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-February 12

Beginniner in python programming

Posted 05 February 2012 - 01:48 PM

I have to write a program to convert kilometers to miles in version 3.2 in python. I get this far in the program and then get a syntax error. Have been working on this for 2 days.
This is due on 2/8/12. Any thoughts?

def main()
   miles = input("Enter a distance in miles: ")     
   kilometers = 1.609 * miles     
   print ("The distance in kilometers is", kilometers) 

main()



Then I get this output and error:

Enter a distance in miles: 1

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    main()
  File "<pyshell#7>", line 3, in main
    kilometers = 1.609 * miles
TypeError: can't multiply sequence by non-int of type 'float'
>>> main()
Enter a distance in miles: 1 * 1.609
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    main()
  File "<pyshell#7>", line 3, in main
    kilometers = 1.609 * miles
TypeError: can't multiply sequence by non-int of type 'float'
>>> def milesToKilometres():     miles = input("Enter a distance in miles: ")     kilometres = 1.609 * miles     print "The distance in kilometres is", kilometres

This post has been edited by Simown: 05 February 2012 - 02:48 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Beginniner in python programming

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2017
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: Beginniner in python programming

Posted 05 February 2012 - 02:31 PM

In python 3.x input always returns a string. So if you want to do arithmetic with it, you'll need to convert it to a number first. You can use the float function to convert a string to a floating point number.
Was This Post Helpful? 2
  • +
  • -

#3 deviljeon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 29-January 12

Re: Beginniner in python programming

Posted 06 February 2012 - 01:20 AM

kilometers = 1.609 * float(miles)

could be your answer..
try learning data type conversion
try it yourself for other functions
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1