#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Help with a basic, four-line python operation


    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    The video you're looking at is using Python 2. In Python 2 the input function evaluates the input it's given, whereas in Python 3 it simply returns it back to you. The difference in how your editor (I'm assuming IDLE) highlights "print" is also related to that difference in versions: in Python 2, print was a keyword that sets off a special statement, whereas in Python 3 it's a normal function. Also, Python doesn't need semicolons unless you want to put multiple statements on one line (which shouldn't anyway).

    In any case, if you want the user to input a number, you'll need to wrap the call to input() in a call to something that converts a string to a number. In particular, float() is probably your best bet.
    Code:
    def miles_to_feet():
        miles = float(input("Enter distance in miles: "))
        feet = 5280 * miles
        print(miles, "miles = ", feet, "feet")

IMN logo majestic logo threadwatch logo seochat tools logo