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');
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top