Help with a basic, four-line python operation

  • Python
  • Thread starter PotentialE
  • Start date
  • #1
57
0

Main Question or Discussion Point

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!
 

Answers and Replies

  • #2
jambaugh
Science Advisor
Insights Author
Gold Member
2,215
248
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.
 
  • #3
57
0
Thanks!

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.
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');
 

Related Threads on Help with a basic, four-line python operation

Replies
7
Views
929
  • Last Post
Replies
3
Views
2K
Replies
1
Views
5K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
5
Views
6K
  • Last Post
Replies
2
Views
2K
Replies
14
Views
1K
Replies
1
Views
2K
Replies
14
Views
3K
Replies
1
Views
3K
Top