Help with a basic, four-line python operation

  • Context: Python 
  • Thread starter Thread starter PotentialE
  • Start date Start date
  • Tags Tags
    Python
Click For Summary
SUMMARY

The discussion centers on a Python 3.3 program designed to convert miles to feet. The initial code incorrectly outputs repeated instances of the input value due to the input being treated as a string. The solution involves converting the input string to a float using the float() function, allowing for proper mathematical operations. The corrected code successfully calculates and displays the conversion from miles to feet.

PREREQUISITES
  • Basic understanding of Python 3.3 syntax
  • Familiarity with data types in Python, specifically strings and floats
  • Knowledge of Python's built-in functions, particularly input() and float()
  • Experience with debugging simple Python scripts
NEXT STEPS
  • Explore Python's built-in functions documentation
  • Learn about type conversion in Python, focusing on str() and float()
  • Investigate common input/output issues in Python programming
  • Practice writing and debugging Python functions for mathematical operations
USEFUL FOR

Beginner Python programmers, educators teaching basic programming concepts, and anyone interested in understanding type conversion and input handling in Python.

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

Similar threads

Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K