Python 小写函数不使用';我不能在不同的功能中工作

Python 小写函数不使用';我不能在不同的功能中工作,python,Python,请容忍我,我是Python新手 我编写的第一个函数是将通过它的字符串中的任何大写字符小写,它自己工作,而忽略任何非大写字母的ASCII字符 但是,当我尝试在第二个函数中使用它时(应该在用户输入的任何内容上使用小写函数,然后将其粘贴到文件中),我只剩下一个文件,其中包含最初传递的字符串,而没有任何小写函数 import os.path from os import path def lowercaser(text): text = [ord(c) for c in text]

请容忍我,我是Python新手

我编写的第一个函数是将通过它的字符串中的任何大写字符小写,它自己工作,而忽略任何非大写字母的ASCII字符

但是,当我尝试在第二个函数中使用它时(应该在用户输入的任何内容上使用小写函数,然后将其粘贴到文件中),我只剩下一个文件,其中包含最初传递的字符串,而没有任何小写函数

import os.path
from os import path


def lowercaser(text):
    text = [ord(c) for c in text]
    length = len(text)

    i = 0
    while length != i:
        if 65 <= text[i] <= 90:
            text[i] = text[i] + 32
        i += 1

    text = [chr(c) for c in text]
    text = "".join(text)


def does_rfid_list_exist():
    if path.exists("rfidList.txt"):
        print("File found!")

    else:
        print("File was not located! Creating new file.\n")
        f = open("rfidList.txt", "a+")
        user_input = input("Please enter your name!\n")
        lowercaser(user_input)
        f.write(user_input)
        f.close()


does_rfid_list_exist()

导入操作系统路径
从操作系统导入路径
def小写字母(文本):
text=[文本中c的ord(c)]
长度=长度(文本)
i=0
而长度!=一:

如果65代码看起来没有异常,那么我看到的唯一问题是,即使您正在调用函数,也没有更新用户输入

user\u input=小写字母(user\u input)

除此之外,从小写函数返回文本
returntext

import os.path
from os import path


def lowercaser(text):
    text = [ord(c) for c in text]
    length = len(text)

    i = 0
    while length != i:
        if 65 <= text[i] <= 90:
            text[i] = text[i] + 32
        i += 1

    text = [chr(c) for c in text]
    text = "".join(text)
    return text #### Change - 1


def does_rfid_list_exist():
    if path.exists("rfidList.txt"):
        print("File found!")

    else:
        print("File was not located! Creating new file.\n")
        f = open("rfidList.txt", "a+")
        user_input = input("Please enter your name!\n")
        user_input = lowercaser(user_input) #### Change - 2
        f.write(user_input)
        f.close()


does_rfid_list_exist()

您似乎正在等待此电话:

lowercaser(user_input)
要更改
用户输入的值
。它不会这样做的。原因是字符串值是不可变的,这意味着每次“更改”字符串时,都会创建一个新的字符串。未触及原始字符串对象。因此,在本例中,当您调用
lowercaser
时,变量
user\u input
指向特定字符串。一旦该函数返回,
user\u input
仍将指向同一字符串。其他一些字符串将作为函数处理的结果而存在

这通常的工作方式是
小写
函数将返回新字符串作为函数的返回值,如下所示:

user_input = lowercaser(user_input)

这样,您将
user\u input
指向一个新字符串,即
lowercaser
为您生成的字符串。但要使其工作,您必须修复
小写字母
函数以返回其结果。因此,您还必须添加
返回文本
作为
小写字母
函数的最后一行。

您需要首先了解如何定义函数。
import os.path
from os import path


def lowercaser(text):
    text = [ord(c) for c in text]
    length = len(text)

    i = 0
    while length != i:
        if 65 <= text[i] <= 90:
            text[i] = text[i] + 32
        i += 1

    text = [chr(c) for c in text]
    text = "".join(text)


def does_rfid_list_exist():
    if path.exists("rfidList.txt"):
        print("File found!")

    else:
        print("File was not located! Creating new file.\n")
        f = open("rfidList.txt", "a+")
        user_input = input("Please enter your name!\n")
        lowercaser(user_input)
        f.write(user_input)
        f.close()


does_rfid_list_exist()

你可以在学校或任何其他来源或书籍中学习

我在这里发布的解决方案,但它不会为您的生产

在函数的最后一行。放

user_input=lowercaser(user_input)
而不是

lowercaser(user_input)

Bruv,没有return语句,即使有,变量也不会存储在任何地方。这意味着文本(只能在其源函数内部访问的局部变量)不可访问、传递或存储在函数does\u rfid\u list\u exist()中。另一个问题是,用户输入(似乎是您试图操作的数据)在使用lowercaser()函数后不会更改,因为没有返回任何内容,数据也没有存储在任何内容中。我要做的是修复

使其能够让用户输入

返回文本 之后 text=”“.join(文本) 替换 小写字母(用户输入) 具有 用户输入=小写字母(用户输入)


我想你可能会感到困惑,认为user\u input是某种公共类。

代码看起来并不奇怪,我看到的唯一问题是,即使你正在调用函数,你也没有更新
user\u input
,同时从小写函数
user\u input=lowercaser>返回
text
(用户输入)
,尝试使用此功能查看Python文档中的。您可能会在那里找到感兴趣的方法。您也可以在
用户输入上本机使用
lower()
函数
lowercaser(user_input)