可能重複:
Python splitting strings and jumbling the middlePython中,定義函數和訪問它
我有蟒蛇,我真的被困在一個程序,我的這個目的是採取從一個用戶的句子,混亂中間的字留下的外部信件完好無損...我的老師說我需要做一些參數,但我真的不知道如何訪問或使用我的功能...
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 MY NMAE IS 導致OUTPUT:H leai埃爾moymns 海ynmeEa流明LS Hiyn升oelEamms ħnaellmEyo m是
沒有,這是相同的程序,但我有不同的錯誤/障礙。 – Magpy
'list(user_words.split())'是多餘的,只需使用'user_words.split()'。 –
如果出現錯誤,請發佈完整回溯。否則,發佈一個示例輸入和期望的輸出 – inspectorG4dget