Python input error

后端 未结 4 739
[愿得一人]
[愿得一人] 2021-01-14 12:10

I\'m running python 2.7.10 on Mac OSX 10.9.5m and it\'s not working. Here\'s the code:

# YourName.py
name = input(\"What is your name?\\n\")
print(\"Hi, \",          


        
4条回答
  •  半阙折子戏
    2021-01-14 12:26

    use raw_input in python 2.7.1:

    name = raw_input("What is your name?\n")
    

    Otherwise you have to rely on the user to know well-enough to input in quoted string. like "David", or the input attempts to evaluate a name (variable/object/etc) and if there is no such name in scope, you'll get the error.

    Alternatively, use exception handling:

    name = input("What is your name?\n")
    try:
        print("Hi, ", name)
    except NameError, e:
        print("Please enclose your input with quotes")
    

提交回复
热议问题