Help with a basic, four-line python operation

  • Thread starter PotentialE
  • Start date
  • Tags
    Python
In summary: def main(): try: import sys
  • #1
PotentialE
57
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
  • #2
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
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');
 

1. What is a basic, four-line python operation?

A basic, four-line python operation is a simple code that is written in the Python programming language and consists of four lines of code. It can perform a specific task or operation, such as printing a message or calculating a mathematical equation.

2. How do I write a basic, four-line python operation?

To write a basic, four-line python operation, you will need to open a Python editor or IDE and type in four lines of code. These lines should include an import statement (if necessary), any necessary variables or values, the main operation or task, and a print statement to display the result.

3. Can you provide an example of a basic, four-line python operation?

Sure! Here is an example of a basic, four-line python operation that calculates the area of a rectangle:

length = 5
width = 3
area = length * width
print("The area of the rectangle is", area)

4. What are some common mistakes when writing a basic, four-line python operation?

Some common mistakes when writing a basic, four-line python operation include forgetting to include necessary variables or values, using incorrect syntax, or not properly indenting the code. It is also important to make sure the code is written in the correct order for the desired outcome.

5. Can I use a basic, four-line python operation for more complex tasks?

Yes, a basic, four-line python operation can be used for more complex tasks by incorporating functions, loops, and conditional statements. However, it is important to keep in mind that for more complex tasks, it may be necessary to write more than four lines of code to achieve the desired outcome.

Similar threads

  • Programming and Computer Science
Replies
2
Views
855
  • Programming and Computer Science
Replies
3
Views
316
  • Programming and Computer Science
Replies
3
Views
718
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
18
Views
2K
Back
Top