【发布时间】:2012-10-21 03:10:47
【问题描述】:
我有一个 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