Input command taking input as an int instead of a str in Python

前端 未结 1 728
情书的邮戳
情书的邮戳 2021-01-24 07:34

I am a beginner programing with Python 2.79. I am writing a program that \"tokenizes\" a mathematical formula, basically turning each number and operator into an item in a list

1条回答
  •  醉梦人生
    2021-01-24 08:10

    The reason Python is interpreting the user's input as an integer is because of the line input('Enter a math equation: '). Python interprets that as eval(raw_input(prompt)). The raw_input function creates a string from the user input, and eval evaluates that input -- so an input of 5+2 is considered "5+2" by raw_input, and eval evaluates that to be 7.

    Documentation

    0 讨论(0)
提交回复
热议问题