我问了一个类似的问题,但无济于事.

我是一名新手编程学生,我只学过一些基本技巧.部分任务是创建一个我主要完成的配方程序,只有一部分阻止我完成.

我应该允许用户调用以前创建的文本文件(我已完成此位),然后在此之后应该显示该文件的内容供他们查看(我也做过这一点),但是用户应该能够重新计算份量,从而改变成分的数量.因此,如果用户输入:“我想要2份”并且1份的原始数量是100g,那么它现在应该输出200g.

这真让我感到沮丧,我的老师明天希望这项工作.以下是我应该允许用户做的事情.

用户应该能够检索配方并为不同数量的人重新计算成分.

•程序应要求用户输入人数.

•程序应输出:

•食谱名称

•新人数

•修订后的数量,包含此人数的单位.

我将在下面发布我的实际代码,以显示我到目前为止所做的工作,即允许用户查看和制作新配方.但缺少修订后的数量.

如果代码混乱或无组织,我很抱歉,我是新手.

代码到目前为止:

#!/usr/bin/env python

import time

def start():

while True:

user_input = input("\nWhat would you like to do? " "\n 1) - Enter N to enter a new recipe. \n 2 - Enter V to view an exisiting recipe, \n 3 - Enter E - to edit a recipe to your liking. \n 4 - Or enter quit to halt the program " "\n ")

if user_input == "N":

print("\nOkay, it looks like you want to create a new recipe. Give me a moment..." "\n")

time.sleep(1.5)

new_recipe()

elif user_input == "V":

print("\nOkay, Let's proceed to let you view an existing recipe stored on the computer")

time.sleep(1.5)

exist_recipe()

elif user_input == "E":

print("\nOkay, it looks like you want to edit a recipe's servings. Let's proceed ")

time.sleep(1.5)

modify_recipe()

elif user_input == "quit":

return

else:

print("\nThat is not a valid command, please try again with the commands allowed ")

def new_recipe():

new_recipe = input("Please enter the name of the new recipe you wish to add! ")

recipe_data = open(new_recipe, 'w')

ingredients = input("Enter the number of ingredients ")

servings = input("Enter the servings required for this recipe ")

for n in range (1,int(ingredients)+1):

ingredient = input("Enter the name of the ingredient ")

recipe_data.write("\nIngrendient # " +str(n)+": \n")

print("\n")

recipe_data.write(ingredient)

recipe_data.write("\n")

quantities = input("Enter the quantity needed for this ingredient ")

print("\n")

recipe_data.write(quantities)

recipe_data.write("\n")

unit = input("Please enter the unit for this quantity (i.e. g, kg) ")

recipe_data.write(unit)

print("\n")

for n in range (1,int(ingredients)+1):

steps = input("\nEnter step " + str(n)+ ": ")

print("\n")

recipe_data.write("\nStep " +str(n) + " is to: \n")

recipe_data.write("\n")

recipe_data.write(steps)

recipe_data.close()

def exist_recipe():

choice_exist= input("\nOkay, it looks like you want to view an existing recipe. Please enter the name of the recipe required. ")

exist_recipe = open(choice_exist, "r+")

print("\nThis recipe makes " + choice_exist)

print(exist_recipe.read())

time.sleep(1)

def modify_recipe():

choice_exist = input("\nOkay, it looks like you want to modify a recipe. Please enter the name of this recipe ")

exist_recipe = open(choice_exist, "r+")

servrequire = int(input("Please enter how many servings you would like "))

start()

编辑:

下面是一个文本文件(配方)的示例创建及其输出(该文件名为bread.txt)注意输出有点乱,我将解决一旦我可以使程序的核心工作.

创建食谱

What would you like to do?

1) - Enter N to enter a new recipe.

2 - Enter V to view an exisiting recipe,

3 - Enter E - to edit a recipe to your liking.

4 - Or enter quit to halt the program

N

Okay, it looks like you want to create a new recipe. Give me a moment...

Please enter the name of the new recipe you wish to add! bread.txt

Enter the number of ingredients 3

Enter the servings required for this recipe 1

Enter the name of the ingredient flour

Enter the quantity needed for this ingredient 300

Please enter the unit for this quantity (i.e. g, kg) g

Enter the name of the ingredient salt

Enter the quantity needed for this ingredient 50

Please enter the unit for this quantity (i.e. g, kg) g

Enter the name of the ingredient water

Enter the quantity needed for this ingredient 1

Please enter the unit for this quantity (i.e. g, kg) l

Enter step 1: pour all ingredients into a bowl

Enter step 2: mix together

Enter step 3: put in a bread tin and bake

查看食谱

What would you like to do?

1) - Enter N to enter a new recipe.

2 - Enter V to view an exisiting recipe,

3 - Enter E - to edit a recipe to your liking.

4 - Or enter quit to halt the program

V

Okay, Let's proceed to let you view an existing recipe stored on the computer

Okay, it looks like you want to view an existing recipe. Please enter the name of the recipe required. bread.txt

This recipe makes bread.txt

Ingrendient # 1:

flour

300

g

Ingrendient # 2:

salt

50

g

Ingrendient # 3:

water

1

l

Step 1 is to:

pour all ingredients into a bowl

Step 2 is to:

mix together

Step 3 is to:

put in a bread tin and bake

如果输入V,这是输出:

这个食谱制作了bread.txt

Ingrendient # 1:

flour

300

g

Ingrendient # 2:

salt

50

g

Ingrendient # 3:

water

1

l

Step 1 is to:

pour all ingredients into a bowl

Step 2 is to:

mix together

Step 3 is to:

put in a bread tin and bake

我期待着你的回复.

解决方法:

您的配方文件看起来像这样:

Ingrendient # N:

{INGREDIENT}

{AMOUNT}

{METRIC}

...

(After N ingredients...)

Step N:

{INSTRUCTIONS}

所以基本上你想要一次读取四行,丢弃第一行,然后将其余部分分配给有用的东西.

with open(path_to_recipe) as infile:

ingredients = []

while True:

try:

sentinel = next(infile) # skip a line

if sentinel.startswith("Step"):

# we're past the ingredients, so

break

name = next(infile)

amount = next(infile)

metric = next(infile)

except StopIteration:

# you've reached the end of the file

break

ingredients.append({'name':name, 'amount':float(amount), 'metric':metric})

# use a dictionary for easier access

当我们退出with块时,成分将是一个字典列表,可以按如下方式使用:

for ingredient in ingredients:

scaled_volume = ingredient['amount'] * scale # double portions? etc...

print(scaled_volume, ingredient['metric'], " ", ingredient['name'])

# # RESULT IS:

300g flour

50g salt

1l water

你应该能够利用所有这些来完成你的任务!

标签:python,file,python-3-x,raspberry-pi

来源: https://codeday.me/bug/20190702/1361141.html

python如何让用户输入文件名并打开文件_(Python)如何让用户打开文本文件然后更改整数/数字...相关推荐

  1. 怎么用java打开文件_如何使用java程序打开一个文件?

    首先应该了解一点:Runtime是Java虚拟机运行时的一个对象,而Java虚拟机运行实际上是操作系统的一个进程而已.通过Runtime对象可以启动其它的子进程,从而返回一个process的对象.说白 ...

  2. Java黑皮书课后题第8章:**8.37(猜测首府)编写一个程序,重复提示用户输入一个州的首府。当接收到用户输入后,程序报告答案是否正确。假设50个州以及它们的首府保存在一个二维数组中,提示用户回答所

    **8.37(猜测首府)编写一个程序,重复提示用户输入一个州的首府 题目 题目描述与运行示例 破题 代码 题目 题目描述与运行示例 **8.37(猜测首府)编写一个程序,重复提示用户输入一个州的首府. ...

  3. 如何让 VSCode 打开文件始终在新标签页打开?

    一. visual-studio-code 这是因为你单击文件名的缘故,这个是"预览模式",所以再单击其他文件时,会覆盖当前打开的文件. 如果你要每次都打开新tab,那就双击文件名 ...

  4. HTML文件总是WPS打开,设置wps默认打开方式_设置默认使用WPS打开文件

    今天有个客户问我,为什么我双击xls文件不能直接用WPS打开,我直接帮他远程调试,发现他wps没有关联默认文件格式. 下面我就教大家如何设置WPS默认打开方式. 测试环境:windows 7  64位 ...

  5. 需要计算机管理员权限才能打开,win7系统打开文件提示需要管理员权限才能打开的解决方法...

    很多小伙伴都遇到过win7系统打开文件提示需要管理员权限才能打开的困惑吧,一些朋友看过网上零散的win7系统打开文件提示需要管理员权限才能打开的处理方法,并没有完完全全明白win7系统打开文件提示需要 ...

  6. python输入文件名读取文件_[Python] python3 文件操作:从键盘输入、打开关闭文件、读取写入文件、重命名与删除文件等...

    1.从键盘输入 Python 2有两个内置的函数用于从标准输入读取数据,默认情况下来自键盘.这两个函数分别是:input()和raw_input(). Python 3中,不建议使用raw_input ...

  7. 编写一个python程序判断用户输入的8位银行卡_用Python编写的程序,提示用户输入一个由7位数字组成的帐号?...

    我在上一门Python入门课程,但有点困在作业上.任何建议或资源将不胜感激!在 问题是: 用Python编写一个程序,提示用户输入由7位数字组成的帐号.在 从用户处获取该帐号后,验证该帐号是否有效.您 ...

  8. python打开文件_用Python(in PsychoPy)打开SPSS数据文件

    用Python(in PsychoPy)打开SPSS数据文件 有时,要访问SPSS的sav文件中的内容,而手里电脑没有SPSS软件,或者需要对SPSS数据文件中的数据进行SPSS支持不够好的操作,如对 ...

  9. python文件打开模式rb表示只读模式打开文件_一篇搞懂python文件读写操作(r/r+/rb/w/w+/wb/a/a+/ab)...

    关于文件操作的几种常用方式,网上已有很多解说,内容很丰富,但也因此有些杂乱复杂.今天,我就以我个人的学习经验写一篇详细又易懂的总结文章,希望大家看完之后会有所收获. 一.各模式逐个分解 'r':只读. ...

最新文章

  1. python引入同目录文件_Python的文件目录操作
  2. 启动64位 IIS 32位应用程序的支持
  3. python学习高级篇(part2)--类方法,静态方法,访问控制
  4. JBPM中 使用JobExecutor执行timer定义的job
  5. you have mixed tabs and spaces fix this
  6. 食堂外卖java源代码,基于jsp的饭堂外卖系统-JavaEE实现饭堂外卖系统 - java项目源码...
  7. 小猴子下落nyoj63(一道可以直接写的好题)
  8. 如何系统嗯学习计算机知识,老师,非科班出身的人该怎么系统的学习计算机知识呢?...
  9. 松柏先生:从《功守道》看电商品牌最后的机会!
  10. 戴尔windows10桌面计算机,戴尔电脑win10怎么在桌面显示我得电脑
  11. gbk英文字符占几个字节?
  12. Spring Security Oauth2 JWT
  13. 双绞线传输器的常见问题解析
  14. 在Ubuntu20.04上安装ros
  15. 习惯养成app_如何培养优秀的开发人员沟通技巧,养成不良习惯
  16. 高通8953平台usb转以太网芯片ax88772驱动
  17. AIS 2019(ACL IJCAI SIGIR)论文研讨会研究趋势汇总
  18. CF14E Camels(暴力dp || 优化dp)
  19. 基于word2vec或doc2vec的情感分析
  20. java毕业设计牙科诊所管理系统Mybatis+系统+数据库+调试部署

热门文章

  1. BUUCTF(pwn)mrctf2020_easyoverflow
  2. python base64库介绍
  3. python中字符串的几种表达方式(用什么方式表示字符串)
  4. loglevel python 不输出_Python 通过 Celery 框架实现分布式任务队列!
  5. php实现页面雪花效果,JavaScript_使用javascript实现雪花飘落的效果,看了javascript网页特效实例大全 - phpStudy...
  6. 24有几种封装尺寸_Y6T16 光模块尺寸演进
  7. C语言函数strcmp()(比较两个字符串)
  8. C语言左移位符号 << 结合 |= 实现置位操作
  9. 蓝桥杯C++ AB组辅导课 第一讲 递归与递推 Acwing
  10. 什么是node网站服务器,node.js