這是我的問題,我必須撰寫一個函式來接收一個字串并回傳該字串,其中的字符為"*"大寫,例如給定該字串: “I want *this text* to be uppercase”,它回傳 : “I want THIS TEXT to be uppercase”。這是我寫的代碼:
l = []
def func(s):
inside = False
for i in s:
if i == "*" and not inside:
inside = True
while inside:
if i == "*":
inside = False
else:
i.upper()
l.append(i)
print(s)
當我運行程式時,它會列印出沒有任何更改的文本。我究竟做錯了什么?謝謝
uj5u.com熱心網友回復:
我想你已經試圖讓這比它更復雜。您需要找到兩個星號的索引。從中您可以獲得原始字串的三個切片并將upper()應用于星號之間的切片。請注意,如果字串中的星號少于兩個,則此代碼將失敗。
def dotricks(s):
i = s.index('*')
j = s.index('*', i i)
return s[0:i] s[i 1:j].upper() s[j 1:]
print(dotricks('I want *this text* to be uppercase'))
uj5u.com熱心網友回復:
您沒有更改代碼中的字串。在下面的這個編輯中,我已將字串的字母分配給一個新變數。并用于continue跳過“*”。此外,最后您的 append 會給您一個您需要.join()用來連接的字母串列。
嘗試對您的代碼進行此編輯,經過測驗并正常作業:
l = []
def func(s):
inside = False
temp = ""
for i in s:
if i == "*" and not inside:
inside = True
continue
if inside:
if i == "*":
inside = False
continue
else:
temp = i.upper()
else:
temp = i
l.append(temp)
new_string = "".join(l)
print(new_string)
return new_string
func("I want *this text* to be uppercase")
uj5u.com熱心網友回復:
這里有幾個問題:
- 你不會在你的函式中回傳任何東西
- 您的
while回圈毫無意義,因為i它內部沒有增加 - 您需要分配
i給i.upper() - 您需要將輸入傳送到串列
更正后的代碼如下:
l = []
def func(s):
inside = False
for i in s:
if i == "*" and not inside:
inside = True
while inside:
if i == "*":
inside = False
else:
i.upper()
if i != "*":
l.append(i)
return l
uj5u.com熱心網友回復:
通過在字符處拆分字串,*然后大寫條目是奇數,然后連接在一起。假設總是有偶數,*所以它應該被認為是一種保留字符。
s = "I want *this text* to be uppercase and *this* as well"
print(' '.join((j.upper() if i%2!=0 else j for i, j in enumerate(s.split('*')))))
輸出
I want THIS TEXT to be uppercase and THIS as well
uj5u.com熱心網友回復:
首先,這個函式對 沒有做任何事情s,所以print(s)將列印輸入的任何內容,不變。
其次,while inside回圈需要在該if陳述句之外- 現在,只有在i星號時才能訪問它。將整個塊的縮進減一,然后更改while inside為if inside- 這樣,該代碼將針對iwhen 的所有值執行inside。
接下來,您需要一種方法來在到達第二個星號或第三個星號時發出信號,依此類推。如果不是分配inside給Trueifi是一個星號并且inside是 False,而是在inside每次達到星號時交換它的值如何?
if i == '*':
inside = not inside
這不需要inside在代碼的第二部分進行更改- 它將所有確定的“我在里面/我在外面”邏輯放在一個地方。
接下來,您已經l在函式范圍之外宣告了一個串列,看起來您想要向其中添加編輯過的字符,因此您想要的答案最終在該串列中。但是你希望你的答案是一個字串,而不是一個串列,在全域范圍內宣告一個串列然后在函式呼叫期間編輯它通常是不好的做法(如果你多次呼叫該函式,串列會變得混亂!) . l = ''在函式的開頭宣告一個空字串,然后您可以使用 =運算子添加它們,而不是向其附加字符。
您還需要確保添加的是大寫版本l = i.upper()或常規版本l = i,取決于是否inside為真。您可以將所有代碼放在陳述句中的if i == '*'行之后,else以捕獲所有i 不是星號的情況。
把它們放在一起,你的函式看起來像這樣:
def func(s):
l = '' # the string that will be returned (the result of the function)
inside = False
for i in s:
if i == "*": # flip value of inside whenever you see an asterisk
inside = not inside
else: # otherwise, add i to the result
if inside:
l = i.upper() # uppercase if between two asterisks
else:
l = i # unchanged if not
return l # and return the modified string
然后,測驗功能:
my_string = "I want *this text* to be uppercase"
my_string_modified = func(my_string)
print(my_string)
print(my_string_modified)
輸出:
I want *this text* to be uppercase
I want THIS TEXT to be uppercase
正如這篇文章的其他答案中所指出的那樣,肯定有更多“高級”方法可以做到這一點,但我希望這個答案有助于澄清您的代碼中出了什么問題,如何修復它,以及一些好的做法是什么時候你寫這種東西。在我看來,以這種方式撰寫代碼是理解如何設計和實作演算法程序的一種非常好的方式。快樂編碼!
uj5u.com熱心網友回復:
我會利用re模塊的力量:
import re
st = "I want *this text* to be uppercase and *this one*"
v = re.findall("\*(.*?)\*", st)
for s in v:
st = st.replace(f'*{s}*', s.upper())
print(st)
輸出:
>>> I want THIS TEXT to be uppercase and THIS ONE
無論如何,重新編輯您的代碼:
def func(s):
l = []
inside = False
for i in s:
if i == "*":
inside = not inside # switch inside on/off when * is found
if inside:
i = i.upper() # upper the character if inside == True
l.append(i)
return l
如果你查看你的原始代碼,部分問題在于以下邏輯:
if i == "*" and not inside:
inside = True # inside is set to True when * is found....
while inside:
if i == "*":
inside = False # and immediately set to False again!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/335588.html
