NameError: name 'A'没有定义。

时间:2022-07-14 18:14:25

QUESTION: Write a program that takes a position on a chess board as a column col and row value row and checks whether or not the position is valid. Remember that the column in a chess board is a letter ranging from A to H (inclusive) and the row is a number between 1 and 8 (inclusive). A3 or E7 are valid inputs but a1 or L5 are not. If both coordinates are valid, such as E2, the programs prints 'The piece is moved to E2.', otherwise it prints 'The position is not valid.'.

问题:编写一个程序,它在棋盘上作为列和行值行,并检查该位置是否有效。记住,棋盘上的一列是字母,从a到H(含),行是1到8(含)的数字。A3或E7是有效的输入,a1或L5不是。如果两个坐标都是有效的,比如E2,那么程序将输出“将工件移动到E2”。,否则它会打印“该位置无效”。

MY ANSWER:

我的回答:

a=raw_input("you move to:")
col = [A, B, C, D, E, F, G]
row = [1, 2, 3, 4, 5, 6, 7]
if (a in col) and (a in row):
 print 'The piece is moved to '%s%d   %input
else:
 print 'The position is not valid.'

ERROR (after plugging in a value like D5)

错误(在插入D5这样的值之后)

Traceback (most recent call last):
  File "<web session>", line 2, in <module>
NameError: name 'A' is not defined

REQUEST:

要求:

Can you direct my on how to fix my program?

你能告诉我怎样修理我的程序吗?

CONTEXT:

背景:

The purpose of this task is to set me up to answer the following QUESTION:

这个任务的目的是让我回答以下问题:

So far, your program used two separate inputs: a column and a row value. Can you modify your previous program (chess problem, version 3) to accept the position on the chess board as a single input. In other words, assume that there is a variable position which stores both the column and the row value of a chess board such as B5. When both the coordinates in the input are valid, for example, c4, the program prints the message The piece is moved to c4.. If the first coordinate is out of range, print The first coordinate is not in the range a-h or A-H!, if the second coordinate is out range, print The second coordinate is not in the range 1 to 8!.

到目前为止,您的程序使用了两个独立的输入:一个列和一个行值。你能修改你以前的程序(国际象棋问题,第3版)以接受棋盘上的位置作为一个输入吗?换句话说,假设有一个变量位置,它同时存储国际象棋棋盘(如B5)的列和行值。当输入中的两个坐标都是有效的时,例如c4,程序会打印出被移动到c4的消息。如果第一个坐标超出了范围,打印第一个坐标不在a-h或a-h范围内!,如果第二个坐标超出范围,打印第二个坐标不在范围1到8!

CLOSE:

关闭:

Thank you for your assistance.

谢谢你的帮助。

5 个解决方案

#1


3  

The problem is here:

这里的问题是:

col = [A, B, C, D, E, F, G]

Your list is supposed to contain strings, so you need to quote each letter just like how you quoted the strings in your print statements. Unquoted, they're being treated as identifiers (for variables, classes, functions, etc) and that's how you end up with your error.

您的列表应该包含字符串,所以您需要引用每个字母,就像您在print语句中引用字符串一样。未引用的,它们被视为标识符(对于变量、类、函数等),这就是导致错误的原因。

Additionally, as mentioned in Voooza's answer, you need to slice/index the input string (a[0] gets the first character which is the letter, and a[1] gets the second character which is the number). Either quote all the numbers in row or cast a[1] into an int by doing int(a[1]) for the comparison to work.

另外,正如Voooza的答案中提到的,您需要对输入字符串进行切片/索引([0]获得第一个字符,即字母,[1]获得第二个字符,即数字)。要么引用行中的所有数字,要么通过执行int([1])将[1]转换为int,以便进行比较。

Finally, since your raw input variable is called a, you need to format your first print statement using a and not input.

最后,由于原始输入变量被称为a,所以需要使用a而不是输入来格式化第一个打印语句。

#2


2  

col = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

Otherwise it looks for variables A, B, C... and can't find them

否则它会寻找变量A B C…,找不到他们

#3


0  

You need to quote each of 'A', 'B', etc, so that they are parsed as strings, rather than identifiers.

您需要引用“A”、“B”等等,以便它们被解析为字符串,而不是标识符。

#4


0  

you need to put chars in quotes

你需要把chars放在引号里

col = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

and you need to use slices

你需要使用切片

if (a[0] in col) and (a[1] in row):
 print 'The piece is moved to '%s%d   %input
else:
 print 'The position is not valid.'

finaly you need to cast input to number or qoute numbers in row as well

最后,您还需要将输入转换为连续的数字或qoute数字

#5


-1  

Complete program must looks like

完整的程序必须看起来像

col = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
row = [1, 2, 3, 4, 5, 6, 7]
pos=raw_input("you move to:")
try:
    x=pos[0] # A1 > A
    y=pos[1] # A1 > 1
    if (x in col) and (int(y) in row):
       print 'The piece is moved to %s' % pos
except:
   print 'The position is not valid.'

#1


3  

The problem is here:

这里的问题是:

col = [A, B, C, D, E, F, G]

Your list is supposed to contain strings, so you need to quote each letter just like how you quoted the strings in your print statements. Unquoted, they're being treated as identifiers (for variables, classes, functions, etc) and that's how you end up with your error.

您的列表应该包含字符串,所以您需要引用每个字母,就像您在print语句中引用字符串一样。未引用的,它们被视为标识符(对于变量、类、函数等),这就是导致错误的原因。

Additionally, as mentioned in Voooza's answer, you need to slice/index the input string (a[0] gets the first character which is the letter, and a[1] gets the second character which is the number). Either quote all the numbers in row or cast a[1] into an int by doing int(a[1]) for the comparison to work.

另外,正如Voooza的答案中提到的,您需要对输入字符串进行切片/索引([0]获得第一个字符,即字母,[1]获得第二个字符,即数字)。要么引用行中的所有数字,要么通过执行int([1])将[1]转换为int,以便进行比较。

Finally, since your raw input variable is called a, you need to format your first print statement using a and not input.

最后,由于原始输入变量被称为a,所以需要使用a而不是输入来格式化第一个打印语句。

#2


2  

col = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

Otherwise it looks for variables A, B, C... and can't find them

否则它会寻找变量A B C…,找不到他们

#3


0  

You need to quote each of 'A', 'B', etc, so that they are parsed as strings, rather than identifiers.

您需要引用“A”、“B”等等,以便它们被解析为字符串,而不是标识符。

#4


0  

you need to put chars in quotes

你需要把chars放在引号里

col = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

and you need to use slices

你需要使用切片

if (a[0] in col) and (a[1] in row):
 print 'The piece is moved to '%s%d   %input
else:
 print 'The position is not valid.'

finaly you need to cast input to number or qoute numbers in row as well

最后,您还需要将输入转换为连续的数字或qoute数字

#5


-1  

Complete program must looks like

完整的程序必须看起来像

col = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
row = [1, 2, 3, 4, 5, 6, 7]
pos=raw_input("you move to:")
try:
    x=pos[0] # A1 > A
    y=pos[1] # A1 > 1
    if (x in col) and (int(y) in row):
       print 'The piece is moved to %s' % pos
except:
   print 'The position is not valid.'