开发者

what is wrong in my python code?

#!usr/bin/python

listofnames = []

names = input("Pls enter how many of names:")
x = 1

for x 开发者_如何学编程in range(0, names):
    inname = input("Enter the name " + str(x))
    listofnames.append(inname)


print listofnames

error

inname = input("Enter the name " + str(x))

File "", line 1, in NameError: name 'Jhon' is not defined


Use raw_input instead. See http://docs.python.org/library/functions.html#raw_input. input will do the same thing as eval(raw_input(prompt)), so entering in Jhon will try to find the symbol Jhon within the file (which doesn't exist). So for your existing script you'd have to input 'Jhon' (notice the set of quotes) in the prompt so the eval will convert the value to a string.

Here's the excerpt warning from the input documentation.

Warning

This function is not safe from user errors! It expects a valid Python expression as input; if the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation. (On the other hand, sometimes this is exactly what you need when writing a quick script for expert use.)

Below is the corrected version:

#!usr/bin/python

# The list is implied with the variable name, see my comment below.
names = []

try:
    # We need to convert the names input to an int using raw input.
    # If a valid number is not entered a `ValueError` is raised, and
    # we throw an exception.  You may also want to consider renaming
    # names to num_names.  To be "names" sounds implies a list of
    # names, not a number of names.  
    num_names = int(raw_input("Pls enter how many of names:"))
except ValueError:
    raise Exception('Please enter a valid number.')

# You don't need x=1. If you want to start your name at 1 
# change the range to start at 1, and add 1 to the number of names.
for x in range(1, num_names+1)):
    inname = raw_input("Enter the name " + str(x))
    names.append(inname)

print names

NOTE: This is for Python2.x. Python3.x has fixed the input vs. raw_input confusion as explained in the other answers.


input gets text from the user which is then interpreted as Python code (hence it's trying to evaluate the thing you entered, Jhon). You need raw_input for both of them and you'll need to convert the number entered (since it's a string) to an integer for your range.

#!usr/bin/python

listofnames = []
names = 0
try:
    names = int(raw_input("Pls enter how many of names:"))
except:
    print "Problem with input"

for x in range(0, names):
    inname = raw_input("Enter the name %d: "%(x))
    listofnames.append(inname)

print listofnames


In python3, input() now works like raw_input(). However to get your code to work with Python3 a couple of changes are still required

#!usr/bin/python3

listofnames = []

names = int(input("Pls enter how many of names:"))
x = 1

for x in range(0, names):
    inname = input("Enter the name " + str(x))
    listofnames.append(inname)


print(listofnames)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜