Python Help with a basic, four-line python operation

  • Thread starter Thread starter PotentialE
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
The discussion centers around a Python 3.3 program designed to convert miles to feet. The initial code incorrectly outputs a repeated string instead of performing the intended calculation due to the input function returning a string. The solution involves converting the input string to a numerical value using the float function. After implementing this change, the program correctly calculates and displays the conversion from miles to feet. Additionally, there is a mention of color differences in the code displayed in the terminal, which is attributed to the terminal program rather than the code itself.
PotentialE
Messages
57
Reaction score
0
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!
 
Technology news on Phys.org
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!

jambaugh said:
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');
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top