修正代码import random def comb(sentence): words input().split('')=() print("-"*60) if len(words)==1 and len(words)==2: print("原句的单词小于等于 2 个") else: jumble=[] while len(words) > 0: site = random.randint (len(words)) jumble.append(words[site]) words= words[:site]+words[(site+1):] s=' '.join(jumble) print("句子打乱顺序后的单词组合为:\n",s) if __name__=="__main__": txt="The Beijing Organising Committee for the 2022 Olympic and Paralympic Winter Games is a public institution with legal person status" print("原句为:\n", txt) comb(txt)
时间: 2024-01-27 15:06:13 浏览: 68
修正后的代码如下:
```python
import random
def comb(sentence):
words = sentence.split(' ') # 修正此处,使用给定的句子进行拆分
print("-"*60)
if len(words)<=2: # 修正此处,判断单词个数小于等于 2 个的情况
print("原句的单词小于等于 2 个")
else:
jumble=[]
while len(words) > 0:
site = random.randint(0, len(words)-1) # 修正此处,产生随机数的范围应该是 0 到 len(words)-1
jumble.append(words[site])
words= words[:site]+words[(site+1):]
s=' '.join(jumble)
print("句子打乱顺序后的单词组合为:\n",s)
if __name__=="__main__":
txt="The Beijing Organising Committee for the 2022 Olympic and Paralympic Winter Games is a public institution with legal person status"
print("原句为:\n", txt)
comb(txt)
```
修正的主要内容如下:
1. 在函数内部,使用给定的句子进行拆分,将其分成单词列表。
2. 修改了判断单词个数小于等于 2 个的条件。
3. 修正了随机数的产生范围,应该是 0 到 len(words)-1。
4. 修改了句子的输出格式。
阅读全文
相关推荐


















