Python中文
首页
教程
问答
标签
搜索
登录
注册
将.txt读入定义
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我试图从一个简单的文本文件中读取如下:</p> <pre><code>11 eggs 1.17 milk 3.54 bread 1.50 coffee 3.57 sugar 1.07 flour 1.37 apple .33 cheese 4.43 orange .37 bananas .53 potato .19 </code></pre> <p>我要做的是请求输入一个文件名,比如“milk”,而不是打印它的价格。我在试着用字典。在</p> <p>这是我的代码:</p> ^{pr2}$ <p><strong>预期输出</strong></p> <p>这个程序会记录你的购物清单。 使用“退出”退出。 输入项目:鸡蛋 你目前的总数是1.17美元</p> <p>输入项目:面包 你目前的总数是3美元</p> <p>输入项目:糖 你目前的总数是4.07美元</p> <p>输入项目:退出 你最后的总数是4.07美元</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>两样东西:鸡蛋+面包==1.17+1.50!=3.00。。。在</p> <p>其次,您可以(对于这样小的文本文件)编写一个例程,通过交替使用奇偶行来预先构建字典,例如键:值对(或者将type()作为str或float进行测试,然后通过这种方式进行计算)。重复遍历文本文件的开销比引用内存中存储的变量要高。在</p> <p>此外,这种方法使第一步的工作变得更简单,因此您可以集中精力在第二步上,并确保您背后的代码是有效的,并且是有意义的。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
当用户用PYTHON设置一个或一个不带值的URL时,他们怎么能输入一个/a的代码呢?
5 回答
当用户登录到站点时,如何显示不同的导航栏
5 回答
当用户登录时,在Flask中向用户显示处理结果
9 回答
当用户的Flask会话结束时,我如何从Redis后端中移除所有Celery结果?
4 回答
当用户的Okta配置文件字段当前为blan时,更新该字段
2 回答
当用户的付款逾期2天时,从Django模型检索数据
7 回答
当用户的消息以问号结尾时,如何让机器人说些什么?
1 回答
当用户的系统上可能也安装了Python 2.7时,如何在用户的系统上运行Python 3脚本?
2 回答
当用户确定打印数量时,使用Matplotlib打印动画
2 回答
当用户离开时是否可以删除整个网页?
1 回答
当用户给出一个单词时如何打印?
7 回答
当用户继续更改TKin中的值(使用trace方法)时,使用Entry并更新输入的条目
4 回答
当用户编辑表单字段时,从Django时间字段中删除秒数
8 回答
当用户被更改时,消息不会来自web套接字
3 回答
当用户访问表单时,如何使表单为只读,而不具有更改权限
9 回答
当用户试图更改对象的值时,使用描述符类引发RuntimeError
4 回答
当用户调整GUI的大小时,是否有方法更改GUI内容的大小?
9 回答
当用户调整风的大小时,pythontkinter小部件的大小会不均匀
1 回答
当用户购买某个类别时,是否查找其他类别的销售?
8 回答
当用户转到上一页时,Django和芹菜插入操作
3 回答