【问题标题】:Python, defining a function and accessing it [duplicate]Python,定义一个函数并访问它[重复]
【发布时间】:2012-10-21 03:10:47
【问题描述】:

可能重复:
Python splitting strings and jumbling the middle

我有一个 python 程序,我真的被困住了,我这样做的目的是从用户那里得到一个句子,然后把单词的中间弄乱,让外面的字母保持不变......我的老师说我需要用参数做一些事情,但我真的不知道如何访问或使用我的函数......

import random


user_words = input("Enter a word or sentence: ") #Gets user input

word = list(user_words.split()) #Creating a word list of the user input


def Jumble_One_Word(x):

    if len(user_words) > 2: #Only need to change words longer than 2

      first_letter = user_words[0] #Takes the first letter out and defines it

      last_letter = user_words[-1] #Takes the last letter out and defines it

      letters = list(user_words[1:-1]) #Takes the rest and puts them into a list

      random.shuffle(letters) #shuffles the list above

      middle_letters = "".join(letters) #Joins the shuffled list

      final_word = "".join([first_letter, middle_letters, last_letter]) #Puts final      word all back  in place as a list

    print(final_word)#Prints out the final word all back together again


Jumbler_Count = -1

for i in range(len(word)):
    Jumbler_Count + 1
    Word1 = Jumble_One_Word(word[Jumbler_Count])

示例输入:你好,我的名字是 预期输出:HLELO 我的 NMAE 是 结果输出:H leai El moymns 海因米埃利斯 海因·奥利姆 H naellmEyo m 是

【问题讨论】:

  • 不,这是同一个程序,但我遇到了不同的错误/障碍。
  • list(user_words.split()) 是多余的,使用user_words.split()即可。
  • 如果您遇到错误,请发布完整的回溯。否则,发布示例输入和预期输出
  • @inspectorG4dget:不一定有追溯,好像只有一个很残忍的老师,根本不解释。
  • @Gil str.split() 应该将字符串拆分为单词列表,这似乎是发布者想要的。

标签: python list function parameters


【解决方案1】:

您需要在函数内部处理x,而您正在处理user_words

import random

user_words = input("Enter a word or sentence: ") #Gets user input

word = user_words.split()  #no need of list()

def Jumble_One_Word(x):

    if len(x) > 2: #Only need to change words longer than 2


      first_letter = x[0] #Takes the first letter out and defines it

      last_letter = x[-1] #Takes the last letter out and defines it

      letters = list(x[1:-1]) #Takes the rest and puts them into a list

      random.shuffle(letters) #shuffles the list above

      middle_letters = "".join(letters) #Joins the shuffled list

      final_word = "".join([first_letter, middle_letters, last_letter]) #Puts finalword all back  in place as a list

      return final_word # better return from function inste


for i in word:                 #you can iterate this way too
    print (Jumble_One_Word(i))

输出:

$ python3 abc.py
Enter a word or sentence: python guido spam
pyhton
giudo
sapm

【讨论】:

  • 我非常喜欢你的方式......但是我得到你好,没有名字无当我输入时:你好,我的名字是
  • Nvm 我只需要反转 lex(x) > 2: 并让它返回单词......然后它就可以工作了。谢谢
【解决方案2】:

你把一些事情混为一谈,让其他事情变得过于复杂。

首先,您应该将代码放在main() 函数中,以明确它从哪里开始。

另外,在这段代码中:

Jumbler_Count = -1

for i in range(len(word)):
    Jumbler_Count + 1
    print Jumble_One_Word(word[Jumbler_Count])

Jumbler_Count 在这里是不必要的,只需遍历单词列表本身即可。

那么让我们创建一个main() 函数:

def main():
    user_words = raw_input("Enter a word or sentence: ") #Gets user input
    words = user_words.split() #Creating a word list of the user input    

    for word in words:
        print Jumble_One_Word(word)

if __name__ == '__main__':
    main()

这里是您的 Jumble_One_Word 函数的略微改动版本:

def Jumble_One_Word(word):
    if len(word) <= 2: #Only need to change words longer than 2
        return word

    first_letter = word[0] #Takes the first letter out and defines it
    last_letter = word[-1] #Takes the last letter out and defines it
    letters = list(word[1:-1]) #Takes the rest and puts them into a list
    random.shuffle(letters) #shuffles the list above
    middle_letters = "".join(letters) #Joins the shuffled list
    return "".join([first_letter, middle_letters, last_letter]) #Puts final      word all back 

请注意,在您的版本中,您实际上从不使用参数x。我将它重命名为word 以使其更清楚它是什么,并且还使用它而不是user_words,这在这里是完全错误的。

我还删除了 print 语句并让函数返回混乱的单词,因为将数据处理和 IO 分开是一种很好的做法。

【讨论】:

  • 只花了大约 2 分钟写了一个像你这样的代码,然后返回查看一堆答案:(
【解决方案3】:

Word1 = Jumble_One_Word(word[Jumbler_Count]) 右侧的= 符号(赋值运算符)是函数调用。您将 word[Jumbler_Count] 作为参数传递给您的函数。

然而,在你的函数中,你将这个参数命名为x...然后不使用它! “jumble one word”中的“one word”就是论据,x。但是您正试图直接从函数内部访问单词列表。

您需要操作x,而不是从函数内部操作user_words

此外,在循环中,您将函数每次迭代的结果分配给同一个变量。每次循环时,您都会覆盖之前的答案,因此一旦您的函数起作用,您将只能看到最后一个答案。尝试在循环之前使用results = [] 制作results 列表,然后使用results.append(Jumble_One_Word(your_argument)),或者将整个函数调用行替换为结果的print 语句,以便立即显示结果。

顺便说一句,您的循环可以大大简化(这是一个使用打印语句策略的示例,但如果您创建该列表,您也可以使用results.append()):

for item in word:
    print Jumble_One_Word(item)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-11
    • 2015-05-16
    • 2018-01-20
    • 1970-01-01
    • 2018-05-15
    • 2022-10-15
    • 2017-11-30
    • 2021-07-20
    相关资源
    最近更新 更多