Python
我制作了一個程式,它讀取用戶的字串輸入,然后要求洗掉一個字符,然后列印沒有用戶選擇的字符的輸入字串。之后,我希望用戶輸入單詞的一個字符,然后代碼將計算該單詞中有多少個所選字符。我正在努力列印正確的答案。
前任。輸入 = 香蕉 | 字符 = 0 | 列印 = 安娜 | 計數字符 = a | 計數 = 3
s = input ('Enter a string: ')
if s == '':
print ('Empty String, please enter a string: ')
else:
d = int(input('Please enter the character to be removed: '))
print('The new string is: ', s[:d] s[d 1:])
i = input ('Enter a character: ')
count = 0
for i in s:
count = count 1
print (count)
Output
Enter a string: banana
Please enter the character to be removed: 0
The new string is: anana
Enter a character: a
6
uj5u.com熱心網友回復:
在增加計數器之前,您需要滿足一個條件。此外,您正在覆寫i回圈中的變數:
for c in s:
if c == i:
count = 1
uj5u.com熱心網友回復:
for 回圈中的 i 與您從用戶那里收到的輸入不同。相反,你應該寫:
l = input ('Enter a character: ')
count = 0
for i in s:
if l == i:
count = 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/337490.html
