简体   繁体   中英

How do I accept math operators without executing them?

My user must input a telephone number. My code works fine if the user enters the number in quotations, but if the user enters it without quotations then Python will calculate the number being entered using arithmetic, accepting the dashes (-) as subtraction signs. How do I fix this?

Code

str(input("enter a phone number please"))

Result

enter a phone number please387-888-8992 '-9493'

Intended Result

enter a phone number please387-888-8992
>>>'387-888-8992'

Use the raw_input() function instead of input() .

input() is the equivalent of eval(raw_input()) ; it tries to interpret all input as Python expressions. raw_input() simply returns the user input as a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM