提问人:Joe Roth 提问时间:9/15/2023 更新时间:9/15/2023 访问量:91
Python 中的 Nato Alphabet 程序 [已关闭]
Nato Alphabet program in Python [closed]
问:
我编写了一个程序,可以获取您的输入,将它们保存在列表中并将它们转换为北约音标等价物,但是,如果我执行多个输入,它仅适用于第一个列表索引,我无法弄清楚我的循环出了什么问题
from pickle import FALSE
# Two lists one for nato the other for letters
import sys
nato= ["alpha", "bravo", "charlie", "delta", "echo", "fox-trot", "golf", "hotel", "india","juliet", "kilo", "lima", "mike", "november", "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform", "victor", "whiskey", "x-ray", "yankee", "zulu" ]
alphabet = ['a','b','c', 'd', 'e','f','g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
# enter input in words or letters
user_input = input('Enter any word or letter: ').casefold() # if the user enters a capital letter
list =[]
list.append(user_input)
print("Continue?: ")
answer = input("y or n: ")
if answer == "y":
answer=True
elif answer =="n":
answer=False
print(list)
while bool(answer) == True:
user_input = input('Enter any word or letter: ').casefold() # if the user enters a capital letter
list.append(user_input)
print("Continue?: ")
answer = input("y or n: ")
if answer == "y":
answer=True
elif answer =="n":
answer=False
# print(list)
break
for x in range(len(list)):
x=0
list[x]=list[x][0]
for n in range(26):
if list[x]==alphabet[n]:
list[x]=nato[n]
x+=1
print(list)
输入任何字词或字母:jhkhjk 继续?: y 或 n:y 输入任意字词或字母:sdfg 继续?: y 或 n:n ['朱丽叶', 'SDFG']
那应该说朱丽叶,塞拉
答:
-1赞
pylll
9/15/2023
#1
您必须更改打印条目的 for 循环:
for x in range(len(list)):
list[x]=list[x][0]
for n in range(26):
if list[x]==alphabet[n]:
list[x]=nato[n]
print(list)
如前所述,问题在于线路重置计数器x=0
评论
0赞
Karl Knechtel
9/15/2023
这是错误的,并且表明了对 Python 中循环工作方式的根本误解。虽然 in OP 的代码不是必需的,但它的效果只会被循环的正常操作所覆盖。 不是特殊的语法;它只是一种创建可迭代值序列的方法。循环不仅仅是循环上的句法糖;它实际上将“计数器”设置为从可迭代输入中获取的值,并受该输入长度的限制,而不是任何算术条件。x += 1
range
for
while
0赞
Karl Knechtel
9/15/2023
相反,问题出在循环的顶部。x=0
0赞
pylll
9/15/2023
是的,你是对的,不知道增量不会影响计数器变量,我更习惯于它有影响的 C。
0赞
Joe Roth
9/15/2023
我把增量拿出来了,它起作用了,我已经习惯了 Java 仅此而已
-2赞
Joe
9/15/2023
#2
如前所述,不应用作变量名称。虽然从技术上讲,您可以将其用作变量名称,但强烈建议不要这样做,并将其视为不良做法。这样做的原因是 Python 中的内置数据类型list
list
list
此外,在您的情况下,字典似乎是一个更合适的数据结构:
# Define a dictionary to map letters to NATO phonetic alphabet words
nato_dict = {
'a': 'alpha', 'b': 'bravo', 'c': 'charlie', 'd': 'delta', 'e': 'echo',
'f': 'fox-trot', 'g': 'golf', 'h': 'hotel', 'i': 'india', 'j': 'juliet',
'k': 'kilo', 'l': 'lima', 'm': 'mike', 'n': 'november', 'o': 'oscar',
'p': 'papa', 'q': 'quebec', 'r': 'romeo', 's': 'sierra', 't': 'tango',
'u': 'uniform', 'v': 'victor', 'w': 'whiskey', 'x': 'x-ray',
'y': 'yankee', 'z': 'zulu'
}
# Initialize an empty list to store the NATO phonetic alphabet words
nato_list = []
# Take user input
while True:
user_input = input('Enter any word or letter: ').casefold()
# Check if the input is not empty and is a letter
if user_input and user_input[0].isalpha():
# Get the first letter of the user input
first_letter = user_input[0]
# Find corresponding NATO phonetic alphabet word
nato_word = nato_dict.get(first_letter)
# Append the result to the list
nato_list.append(nato_word)
# Ask if the user wants to continue
answer = input("Continue (y/n): ").strip().lower()
if answer != "y":
break
# Print the NATO phonetic alphabet words
print(nato_list)
如果你好奇如何使用两个列表来获得相同的结果,这里有一个例子:
# Define lists to store letters and their corresponding NATO phonetic alphabet words
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
nato_words = ['alpha', 'bravo', 'charlie', 'delta', 'echo', 'fox-trot', 'golf', 'hotel', 'india', 'juliet', 'kilo', 'lima', 'mike', 'november', 'oscar', 'papa', 'quebec', 'romeo', 'sierra', 'tango', 'uniform', 'victor', 'whiskey', 'x-ray', 'yankee', 'zulu']
# Initialize an empty list to store the NATO phonetic alphabet words
nato_list = []
# Take user input
while True:
user_input = input('Enter any word or letter: ').casefold()
# Check if the input is not empty and is a letter
if user_input and user_input[0].isalpha():
# Get the first letter of the user input
first_letter = user_input[0]
# Find the index of the letter in the 'letters' list
index = letters.index(first_letter)
# Find corresponding NATO phonetic alphabet word from the 'nato_words' list using the same index
nato_word = nato_words[index]
# Append the result to the list
nato_list.append(nato_word)
# Ask if the user wants to continue
answer = input("Continue (y/n): ").strip().lower()
if answer != "y":
break
# Print the NATO phonetic alphabet words
print(nato_list)
评论
list
list[x]
list[x][0]
alphabet
nato
alphabet.find()
for
for x in range(len(list)):
x