Python CS圈编码练习:26个字母

Python CS圈编码练习:26个字母,python,computer-science,Python,Computer Science,这个编码练习给了我很多麻烦,我需要帮助 问题就在这里 编写一个与上面例子相反的程序:它应该 将一个字符作为输入并输出相应的数字(介于 1和26)。您的程序应该只接受大写字母。作为 检查错误,如果输入不是大写字母,则打印无效 这是我到目前为止所拥有的 inp = input() if (len(inp) > 1 or inp != inp.upper()): print("invalid input") else: print(ord(inp)-ord("A")+1)

这个编码练习给了我很多麻烦,我需要帮助

问题就在这里

编写一个与上面例子相反的程序:它应该 将一个字符作为输入并输出相应的数字(介于 1和26)。您的程序应该只接受大写字母。作为 检查错误,如果输入不是大写字母,则打印无效

这是我到目前为止所拥有的

inp = input()

if (len(inp) > 1 or inp != inp.upper()):
    print("invalid input")

else:
    print(ord(inp)-ord("A")+1)

正如评论中所阐明的,问题似乎在于验证没有涵盖足够的案例

要检查您计算的数字是否为有效的大写字符,您只需检查它是否介于1和26之间:

if 1 < ord(inp) - ord("A") + 1 < 26:
    print(ord(inp) - ord("A") + 1)
else:
    print("Invalid input")
如果1
要将其添加到当前验证中,请执行以下操作:

inp = input()

if (len(inp) > 1 or inp != inp.upper() or 
    ord(inp) - ord("A") + 1 < 1 or ord(inp) - ord("A") + 1 > 26):
    print("invalid input")

else:
    print(ord(inp)-ord("A")+1)
inp=input()
如果(len(inp)>1或inp!=inp.upper()或
作战需求文件(inp)-ord(“A”)+1<1或ord(inp)-ord(“A”)+1>26):
打印(“无效输入”)
其他:
打印(ord(inp)-ord(“A”)+1)

如评论中所述,问题似乎在于验证没有涵盖足够的案例

要检查您计算的数字是否为有效的大写字符,您只需检查它是否介于1和26之间:

if 1 < ord(inp) - ord("A") + 1 < 26:
    print(ord(inp) - ord("A") + 1)
else:
    print("Invalid input")
如果1
要将其添加到当前验证中,请执行以下操作:

inp = input()

if (len(inp) > 1 or inp != inp.upper() or 
    ord(inp) - ord("A") + 1 < 1 or ord(inp) - ord("A") + 1 > 26):
    print("invalid input")

else:
    print(ord(inp)-ord("A")+1)
inp=input()
如果(len(inp)>1或inp!=inp.upper()或
作战需求文件(inp)-ord(“A”)+1<1或ord(inp)-ord(“A”)+1>26):
打印(“无效输入”)
其他:
打印(ord(inp)-ord(“A”)+1)

这可能会对您有所帮助

将要测试的内容添加到
test\u数据
列表中

# take advantage of the string module to get a string of all upper-case characters
from string import ascii_uppercase # equiv of 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

test_data = ['z', 'AB', '', 10, None, "@", *ascii_uppercase] # some test data including all the upper-case characters

for item in test_data:
    string = str(item) # turn the item into type str
    length_is_one = len(string) == 1 # get the length
    if all((length_is_one, string in ascii_uppercase)): # if all these are true
        print(ascii_uppercase.index(item) + 1) # print the index of the letter + 1
    else:
        print(f'"{item}" is {INVALID}') # I modified the invalid output
输出:

"z" is invalid
"AB" is invalid
"" is invalid
"10" is invalid
"None" is invalid
"@" is invalid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

这可能对你有所帮助

将要测试的内容添加到
test\u数据
列表中

# take advantage of the string module to get a string of all upper-case characters
from string import ascii_uppercase # equiv of 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

test_data = ['z', 'AB', '', 10, None, "@", *ascii_uppercase] # some test data including all the upper-case characters

for item in test_data:
    string = str(item) # turn the item into type str
    length_is_one = len(string) == 1 # get the length
    if all((length_is_one, string in ascii_uppercase)): # if all these are true
        print(ascii_uppercase.index(item) + 1) # print the index of the letter + 1
    else:
        print(f'"{item}" is {INVALID}') # I modified the invalid output
输出:

"z" is invalid
"AB" is invalid
"" is invalid
"10" is invalid
"None" is invalid
"@" is invalid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

您当前的解决方案有什么问题?输入:@程序执行时没有崩溃。程序输出:0------------------当输入@符号时,它不会打印无效预期此正确输出:分级结果无效:您的输出不正确。您当前的解决方案有什么问题?输入:@程序未崩溃执行。程序输出:0 ---------------------------------------------------------------------------------------------------------------------------------------------------------------Hipon No发热,如果这个或任何答案已经解决了你的问题,请点击点击复选标记来接受它。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。(Hi @ Halpon Doad)如果这个或任何答案已经解决了你的问题,请考虑通过点击复选标记来接受它。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。()