Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with a basic, four-line python operation

  1. Apr 17, 2013 #1
    I am creating a program in Python 3.3 that converts miles to feet, my code is as follows:
    def Miles2Feet():
    Miles = input('Enter distance in miles: ');
    Feet = 5280 * Miles;
    print (Miles, 'Miles =', Feet, 'Feet');

    When I run this using 1 as an input number of miles, it does not display 5280 feet, it displays 5280 1's and then the word feet. How do I fix this so that it does math instead of just listing the input 5280 times? I found the shell for this on a youtube video and copied it exactly, but there must be some additional specification for the version of python that I'm using. Also, on the video, the last "print" is in orange and it is in purple on my screen. The first "input" is also in purple.

    Thanks!
     
  2. jcsd
  3. Apr 17, 2013 #2

    jambaugh

    User Avatar
    Science Advisor
    Gold Member

    I don't know about the color, that's an issue with your terminal program. What is happening is that the input function sets Miles to the text string "1". As a list strings can be multiplied by integers 5*'A' = 'AAAAA'. A nice feature but it is the reason you get the output you do instead of an error. You have to convert the text string input to a numerical value. I'd have to look that up but leave it to you as that's how you learn. Check the documentation on the built in functions.
     
  4. Apr 17, 2013 #3
    Thanks!

    Thanks! I fixed it (literally one minute before you posted this) by using float:

    def Miles2Feet():
    Miles = input('Enter distance in miles: ');
    Miles = float(Miles)
    Feet = 5280 * Miles;
    print (Miles, 'Miles =', Feet, 'Feet');
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Help with a basic, four-line python operation
  1. Basic Python questions (Replies: 5)

  2. Basic python help? (Replies: 3)

  3. Basic python question (Replies: 2)

Loading...