Why can't I define a variable like "miles_driven" with an underscore in Python 3.4.3 ?

Michael Selik michael.selik at gmail.com
Wed Aug 10 14:07:52 EDT 2016


On Wed, Aug 10, 2016 at 1:41 PM Cai Gengyang <gengyangcai at gmail.com> wrote:

> I managed to get this piece of code to work :
>
> >>> print("This program calculates mpg.")
> This program calculates mpg.
> >>> milesdriven = input("Enter miles driven:")
> Enter miles driven: 50
> >>> milesdriven = float(milesdriven)
> >>> gallonsused = input("Enter gallons used:")
> Enter gallons used: 100
> >>> gallonsused = float(gallonsused)
> >>> gallonsused = float(gallonsused)
> >>> mpg = milesdriven / gallonsused
> >>> print("Miles per gallon:", mpg)
> Miles per gallon: 0.5
> >>> print("This program calculates mpg.")
> This program calculates mpg.
> >>> milesdriven = input("Enter miles driven:")
> Enter miles driven: 100
> >>> milesdriven = float(milesdriven)
> >>> gallonsused = input("Enter gallons used:")
> Enter gallons used: 500
> >>> gallonsused = float(gallonsused)
> >>> mpg = milesdriven / gallonsused
> >>> print("Miles per gallon:", mpg)
> Miles per gallon: 0.2
>
> But, why can't i define a variable like "miles_driven" with an underscore
> in my Python Shell ? When I try to define it , the underscore doesn't
> appear at all and instead I get this result :
> "miles driven" , with no underscore appearing even though I have already
> typed "_" between "miles" and "driven" ?
>

You'll have to give more information about the system you're using.
Underscores are allowed in Python variable names.

The first thought I have is that something outside of Python is changing
your underscore to a space. Perhaps your OS spelling autocorrect is overly
aggressive?



More information about the Python-list mailing list