带有单词列表的 Gematria

发布于 2024-12-28 01:06:43 字数 528 浏览 3 评论 0原文

我是编程新手。我试图将一个数字(由用户给出)与文件中单词的数值进行匹配。示例a=1。 b=2, c=3, A=1, B=2,这样如果用户输入“2”,那么输出将是列表中匹配 2 的所有单词。

userinput = raw_input("Please, enter the gematric value of the word: ")
inputfile = open('c:/school/dictionarytest.txt', 'r')
inputfile.lower()
output = []
for word in inputfile:
    userinput = ord(character) - 96
    output.append(character)
    print output
inputfile.close()

我对此有点陌生,语法不是那个熟悉的。有人可以帮忙吗?谢谢

编辑1-示例用户输入数字 7。如果单词 bad (b=2,a=1,d=4) 位于列表中,则输出将为“bad”,并且与添加的任何其他单词相匹配他们的性格。

I am new at programming. I am trying to match a number (given by the user) to the numeric values of words in a file. Example a=1. b=2, c=3, A=1, B=2, so that if the user enters "2" then the output would be all words in the list that match 2.

userinput = raw_input("Please, enter the gematric value of the word: ")
inputfile = open('c:/school/dictionarytest.txt', 'r')
inputfile.lower()
output = []
for word in inputfile:
    userinput = ord(character) - 96
    output.append(character)
    print output
inputfile.close()

I am somewhat new at this and the syntax is not that familiar. Could someone please help? Thank you

Edit1- example the user enters the number 7. If the word bad (b=2,a=1,d=4) is on the list the output would be "bad", and any other words that match the addition of their characters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似最初 2025-01-04 01:06:43

下面是带有详细描述的注释的代码:

# ask user for an input until an integer is provided
prompt = "Please, enter the gematric value of the word: "
while True: # infinite loop
    try:        
        # ask user for an input; convert it to integer immediately
        userinput = int(raw_input(prompt))
    except ValueError: # `int()` can't parse user input as an integer
        print('the gematric value must be an integer. Try again')
    else:
        break # got an integer successfully; exit the loop

# use `with` statement to close the file automatically
# `'r'` is default; you don't need to specify it explicitly
with open(r'c:\school\dictionarytest.txt') as inputfile:
    #XXX inputfile.lower() # WRONG!!! file object doesn't have .lower() method

    # assuming `dictionarytest.txt` has one word per line
    for word in inputfile: # read the file line by line
        word = word.strip() # strip leading/trailing whitespace
        if gematric_value(word) == userinput:
           print(word) # print words that match user input

其中 gematric_value() 函数是:

def gematric_value(word):
    """Sum of numerical values of word's characters.

    a -> 1, b -> 2, c -> 3; A -> 1, B -> 2, etc
    """
    # word is a string; iterating over it produces individual "characters"
    # iterate over lowercased version of the word (due to A == a == 1)
    return sum(ord(c) - ord('a') + 1 for c in word.lower())

注意:不要在代码中使用上述注释样式。仅出于教育目的才可以接受。您应该假设您的代码的读者熟悉 Python。

Here's the code with comments that describe it in detail:

# ask user for an input until an integer is provided
prompt = "Please, enter the gematric value of the word: "
while True: # infinite loop
    try:        
        # ask user for an input; convert it to integer immediately
        userinput = int(raw_input(prompt))
    except ValueError: # `int()` can't parse user input as an integer
        print('the gematric value must be an integer. Try again')
    else:
        break # got an integer successfully; exit the loop

# use `with` statement to close the file automatically
# `'r'` is default; you don't need to specify it explicitly
with open(r'c:\school\dictionarytest.txt') as inputfile:
    #XXX inputfile.lower() # WRONG!!! file object doesn't have .lower() method

    # assuming `dictionarytest.txt` has one word per line
    for word in inputfile: # read the file line by line
        word = word.strip() # strip leading/trailing whitespace
        if gematric_value(word) == userinput:
           print(word) # print words that match user input

Where gematric_value() function is:

def gematric_value(word):
    """Sum of numerical values of word's characters.

    a -> 1, b -> 2, c -> 3; A -> 1, B -> 2, etc
    """
    # word is a string; iterating over it produces individual "characters"
    # iterate over lowercased version of the word (due to A == a == 1)
    return sum(ord(c) - ord('a') + 1 for c in word.lower())

Note: don't use the above comment style in your code. It is acceptable only for educational purposes. You should assume that a reader of your code is familiar with Python.

靖瑶 2025-01-04 01:06:43

您没有读取该文件

inputfile = open('c:/school/dictionarytest.txt', 'r')
file_input = inputfile.readlines().lower()
for character in file_input:
          if userinput == ord(character)-96:
                    output.append(character)

You are not reading the file

inputfile = open('c:/school/dictionarytest.txt', 'r')
file_input = inputfile.readlines().lower()
for character in file_input:
          if userinput == ord(character)-96:
                    output.append(character)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文