Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NameError: name 'z' is not defined #2

Open
OctopusLian opened this issue Jul 20, 2019 · 2 comments
Open

NameError: name 'z' is not defined #2

OctopusLian opened this issue Jul 20, 2019 · 2 comments

Comments

@OctopusLian
Copy link
Owner

运行7-10程序:

name_prompt = "\nWhat's your name? "
place_prompt = "If you could visit one place in the world, where would it be? "
continue_prompt = "\nWould you like to let someone else respond? (yes/no) "

responses = {}

while True:
    name = input(name_prompt)
    place = input(place_prompt)

    responses[name] = place

    repeat = input(continue_prompt)
    if repeat != 'yes':
        break

print("\n--- Results ---")
for name, place in responses.items():
    print(name.title() + " would like to visit " + place.title() + ".")

报错信息

$ python -u 7-10.py 

What's your name? z
Traceback (most recent call last):
  File "7-10.py", line 8, in <module>
    name = input(name_prompt)
  File "<string>", line 1, in <module>
NameError: name 'z' is not defined
@OctopusLian
Copy link
Owner Author

难道是因为输入的时候类型是"z"导致的,而不是z

@OctopusLian
Copy link
Owner Author

OctopusLian commented Jul 21, 2019

input换成raw_input就可以了。
书上是python3版本,而我本机是python2版本。
网上教程简易python程序NameError: name 'xxx' is not defined?

解决方案

  • 1,把input换成raw_input
  • 2,本机更新成python3版本。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant