fruits = {"apple":10,"mango":12,"durian":20,"banana":5} a,b=(input()).split(’,’)#请输入新进的水果名称及报价腾 fruits[a]= ()请将括号补全for m in fruits: I print('{}:{}'.format(m,fruits[a]))
时间: 2023-07-19 19:08:11 浏览: 54
在第4行中,需要将括号补全。因此,将`fruits[a]=`改为`fruits[a]=int(b)`即可。
改进后的代码如下所示:
```
fruits = {"apple":10, "mango":12, "durian":20, "banana":5}
a, b = (input()).split(',') # 请输入新进的水果名称及报价
fruits[a] = int(b)
for m in fruits:
print('{}:{}'.format(m, fruits[m]))
```
相关问题
以下字典fruits中是某水果店现有的水果报价,假设此水果店又新进了一种水果,将该种水新进的水果及其报价由键盘输入,以逗号分隔,然后输出水果店所有水果及其报价。完善代输入输出示例:示例1 输入:杨桃,15输出: apple:10 mango:12 durian:20 P banana:5杨桃:15示例2 输入:草莓,30输出: apple:10 mango:12 6 durian:20 banana:5草莓:30用python
```
fruits = {'apple': 10, 'mango': 12, 'durian': 20, 'banana': 5}
new_fruit, price = input("请输入新进水果及其报价,以逗号分隔:").split(',')
fruits[new_fruit] = int(price)
for fruit, price in fruits.items():
print(fruit + ':' + str(price), end=' ')
```
运行示例:
```
请输入新进水果及其报价,以逗号分隔:杨桃,15
apple:10 mango:12 durian:20 banana:5 杨桃:15
请输入新进水果及其报价,以逗号分隔:草莓,30
apple:10 mango:12 durian:20 banana:5 草莓:30
```
以下字典fruits中是某水果店现有的水果报价,假设此水果店又新进了一种水果,将该种水新进的水果及其报价由键盘输入,以逗号分隔,然后输出水果店所有水果及其报价。完善代 输入输出示例: 示例1 输入:杨桃,15 输出: apple:10 mango:12 durian:20 banana:5 杨桃:15 示例2 输入:草莓,30 输出: apple:10 mango:12 durian:20 banana:5 草莓:30 ,,,
可以使用以下Python代码实现:
```python
fruits = {'apple': 10, 'mango': 12, 'durian': 20, 'banana': 5}
# 从键盘输入新水果及其报价
new_fruit, price = input("请输入新水果及其报价,以逗号分隔:").split(',')
price = int(price)
# 将新水果及其报价添加到字典中
fruits[new_fruit] = price
# 输出所有水果及其报价
for fruit, price in fruits.items():
print(fruit + ':' + str(price))
```
示例1 输入:
```
请输入新水果及其报价,以逗号分隔:杨桃,15
```
输出:
```
apple:10
mango:12
durian:20
banana:5
杨桃:15
```
示例2 输入:
```
请输入新水果及其报价,以逗号分隔:草莓,30
```
输出:
```
apple:10
mango:12
durian:20
banana:5
草莓:30
```
阅读全文