代码工作,直到更多的被添加现在得到一个名字

2024-09-30 06:30:11 发布

您现在位置:Python中文网/ 问答频道 /正文

# -*- coding: utf-8 -*-
#displays title

print ("          H___________________________________________   ")
print ("/========/| █░█ █▀▀█ █▀▀█ █░░ █▀▀█ █▀▀▄ █▀▀▄ \ ")
print ("|||||||||||-█̶▀̶▄̶ ̶█̶░̶░̶█̶ ̶█̶░̶░̶█̶ ̶█̶░̶░̶ ̶█̶▄̶▄̶█̶ ̶█̶░̶░̶█̶ ̶█̶░̶░̶█̶ -\ ")
print ("\========\|_▀̲░̲▀̲ ̲▀̲▀̲▀̲▀̲ ̲▀̲▀̲▀̲▀̲ ̲▀̲▀̲▀̲ ̲▀̲░̲░̲▀̲ ̲▀̲░̲░̲▀̲ ̲▀̲▀̲▀̲░̲ __\ ")
print ("          H ")
print ("          = ")

#intro
print ("Welcom to the land of kool")

#asks your name
name = input ("Can you tell me your name!")

#says hello
print ("Well hello",name,"!")

显示错误:

NameError: name '______' is not defined

Tags: ofthetonamehelloyourtitleutf
2条回答

根据您使用的Python版本,您可能需要使用。。你知道吗

name = raw_input("Can you tell me your name!")

看看这个以供参考。 What's the difference between raw_input() and input() in python3.x?

TL;博士

在python2.x中使用raw_input()而不是input(),因为input()eval(raw_input))的“速记”。你知道吗


在python2.7中,需要使用raw_input()而不是input()raw_input()允许您以字符串的形式读入用户输入,而input()eval(raw_input())的“速记”,它试图以文本Python代码的形式评估用户输入

Python2.x文档中对此进行了说明:

[input() is] Equivalent to eval(raw_input(prompt)).

This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.

If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.

Consider using the raw_input() function for general input from users.

但是,这一点后来在python3.x中发生了更改。在python3.x中,raw_input()变成了input(),旧的输入(eval(raw_input()))被删除。这在What's new in Python 3中有记载:

PEP 3111: raw_input()was renamed toinput(). That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(input()).

(我的重点)


所以在python2.x中使用raw_input()而不是input(),因为input()eval(raw_input))的“速记”。例如,改变:

name = input ("Can you tell me your name!")

name = raw_input ("Can you tell me your name!")

相关问题 更多 >

    热门问题