in_str = input("请输入:");怎么添加剔除空白字符发送数据
时间: 2024-02-11 19:06:21 浏览: 19
您可以使用字符串的strip()方法来剔除输入字符串中的空白字符,然后再发送数据。示例代码如下:
```
in_str = input("请输入:").strip()
# 在这里可以将剔除空白字符后的in_str发送出去
```
strip()方法将会剔除输入字符串开头和结尾的空白字符(包括空格、制表符、换行符等)。如果您还想剔除字符串中间的空白字符,可以使用replace()方法替换空白字符为空字符串,例如:
```
in_str = input("请输入:").replace(" ", "").replace("\t", "").replace("\n", "")
# 在这里可以将剔除空白字符后的in_str发送出去
```
这样就可以将输入字符串中所有的空白字符都剔除掉了。
相关问题
in_str = input("请输入:");如何将输入数据换行
你可以在字符串的末尾添加换行符"\n",例如:
```
in_str = input("请输入:") + "\n"
```
这样用户输入的数据就会自动换行。
score_dict = {}while True: input_str = input().strip() if input_str == '#': break name, score = input_str.split() score = int(score) if name in score_dict: score_dict[name].append(score) else: score_dict[name] = [score]print('姓 名 平均成绩')for name, scores in score_dict: avg_score = sum(scores) / len(scores) print('{:<8} {:.2f}'.format(name, avg_score))
这段代码存在一些问题,应该修改为:
```python
score_dict = {}
while True:
input_str = input().strip()
if input_str == '#':
break
name, score = input_str.split()
score = int(score)
if name in score_dict:
score_dict[name].append(score)
else:
score_dict[name] = [score]
print('姓 名 平均成绩')
for name, scores in score_dict.items():
avg_score = sum(scores) / len(scores)
print('{:<8} {:.2f}'.format(name, avg_score))
```
主要修改包括:
- 对于 `for name, scores in score_dict`,应该改为 `for name, scores in score_dict.items()`,因为字典类型需要使用 `items()` 方法来遍历。
- 对于计算平均成绩,应该在遍历时进行,而不是在输入时计算。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)