在Python中,你可以使用`for`循环或`while`循环来重复输出一句话。以下是两种方法的示例:
使用`for`循环
```python
def repeat_sentence(sentence, times):
for _ in range(times):
print(sentence)
调用函数,输出"Hello, World!" 5次
repeat_sentence("Hello, World!", 5)
使用`while`循环
```python
def repeat_sentence(sentence, times):
count = 0
while count < times:
print(sentence)
count += 1
调用函数,输出"Hello, World!" 5次
repeat_sentence("Hello, World!", 5)
在这两个例子中,`repeat_sentence`函数接受一个字符串`sentence`和一个整数`times`作为参数,然后根据`times`的值重复打印`sentence`。`for`循环使用`range(times)`生成一个序列,`while`循环则使用一个计数器`count`来控制循环次数