April 17th, 2013, 08:40 PM
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:
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.
April 17th, 2013, 10:48 PM
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.
miles = float(input("Enter distance in miles: "))
feet = 5280 * miles
print(miles, "miles = ", feet, "feet")