大家好,所以我的程序在一定程度上可以正常工作。我的程序假设检查用户输入中是否有"A"
,如果这样做,它将用下一个字母交换该"A"
。
以下是示例:
"tan" = "TNA"
"abracadabra" = "BARCADABARA"
"whoa" = "WHOA"
"aardvark" = "ARADVRAK"
"eggs" = "EGGS"
"a" = "A"
在我的情况下,这是行得通的和不行的:
作品:
tan to TNA
不起作用:
abracadabra = BARCADABAR
whoa = WHO
aardvark = ARADVRA
eggs = EGG
a =
a
等于零。
我的意思是最后一个字符没有打印,我不确定该怎么做。
def scrambleWord(userInput):
count = 0
Word_ = ""
firstLetter_ = ""
secondLetter_ = ""
while count < len(userInput):
if count+1 >=len(userInput):
break #copy last character
firstLetter_ = userInput[count] #assigning first letter
secondLetter_ = userInput[count+1] #assigning next letter
if firstLetter_ == 'A' and secondLetter_ != 'A':
Word_ += (secondLetter_ + firstLetter_) #Swap then add both letters
count+=1
else:
Word_+=firstLetter_
count+=1
return Word_
def main():
userInput = input("Enter a word: ")
finish = scrambleWord(userInput.upper())
print(finish)
main()
答案 0 :(得分:1)
可能是因为您只是在没有将userinput [count]写入单词的情况下中断。
if count+1 >=len(userInput):
Word_ += userInput[count]
break #copy last character
这应该有帮助