" print('原文本内容:{}'.format(str)) print('1.删除空格') print('2.英文标点替换') print('3.字母大写') print('4.退出') while True: option = input("请输入功能选项:\n") if option=='1': # 删除所有空格 #newstr = ?????? print('删除所有空格后:',newstr) elif option =='2': # 替换英文标点 #?????????? print('替换英文标点后:',newstr) elif option =='3': # 所有字母大写用upper()实现 #?????????????? print('所有字母大写后:',newstr) elif option == '4': break
时间: 2023-06-07 15:07:31 浏览: 48
这段代码包含菜单选项和一个无限循环,当用户选择菜单选项1时,程序将删除文本中的空格。如果用户选择菜单选项2,程序将替换文本中的英文标点。如果用户选择菜单选项3,程序将将文本中的所有字母转换为大写。最后,如果用户选择菜单选项4,程序将退出循环并结束执行。
相关问题
str = "他问, 你 知 道cba和nba是什么单词的缩写么 ? " print('原文本内容:{}'.format(str)) print('1.删除空格') print('2.英文标点替换') print('3.字母大写') print('4.退出') while True: option = input("请输入功能选项:\n") if option=='1': # 删除所有空格 #newstr = ?????? print('删除所有空格后:',newstr) elif option =='2': # 替换英文标点 #?????????? print('替换英文标点后:',newstr) elif option =='3': # 所有字母大写用upper()实现 #?????????????? print('所有字母大写后:',newstr) elif option == '4': break
str = "他问, 你 知 道cba和nba是什么单词的缩写么 ? "
print('原文本内容:{}'.format(str))
print('1.删除空格')
print('2.英文标点替换')
print('3.字母大写')
print('4.退出')
while True:
option = input("请输入功能选项:\n")
if option == '1':
str = str.replace(' ', '')
print(str)
elif option == '2':
str = str.replace(',', '').replace('?', '').replace(' ', '')
print(str)
elif option == '3':
str = str.upper()
print(str)
elif option == '4':
break
else:
print("请输入正确的选项!")
题目 编写一个程序来计算输入的英文句子中单词的频率。 按字母顺序对键进行排序后输出。不统计各种标点符号:!@#$%^&*,;:'?{[]/}. 输入样例:God is impartial , the people who has destiny in hand stands forever in thebalance both ends , the people who is grasped by destiny knows that God best ows destiny on him only. 输出 应该是:每行输出一个词 及其出现次数,用冒号分隔 God:2 best:1 both:1 by:1 destiny:3 ends:1 forever:1 grasped:1 hand:1 has:1 him:1 impartial:1 in:2 is:2 knows:1 on:1 only.:1 ows:1 people:2 stands:1 that:1 the:2 thebalance:1 who:2
以下是 Python 代码实现:
```python
import string
# 读入句子并去除标点符号
sentence = input().translate(str.maketrans("", "", string.punctuation))
# 将句子按照空格分割成单词
words = sentence.lower().split()
# 统计单词频率
freq = {}
for word in words:
if word in freq:
freq[word] += 1
else:
freq[word] = 1
# 按照字母顺序对单词进行排序并输出
for word in sorted(freq.keys()):
print("{}:{}".format(word, freq[word]))
```
输入样例:
```
God is impartial , the people who has destiny in hand stands forever in the balance both ends , the people who is grasped by destiny knows that God bestows destiny on him only.
```
输出样例:
```
God:2
balance:1
bestows:1
both:1
by:1
destiny:3
ends:1
forever:1
grasped:1
hand:1
has:1
him:1
impartial:1
in:2
is:2
knows:1
on:1
only:1
people:2
stands:1
that:1
the:2
thebalance:1
who:2
```