日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2025/3/19 python 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python如何让用户输入文件名并打开文件_(Python)如何让用户打开文本文件然后更改整数/数字... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我問了一個類似的問題,但無濟于事.

我是一名新手編程學生,我只學過一些基本技巧.部分任務是創建一個我主要完成的配方程序,只有一部分阻止我完成.

我應該允許用戶調用以前創建的文本文件(我已完成此位),然后在此之后應該顯示該文件的內容供他們查看(我也做過這一點),但是用戶應該能夠重新計算份量,從而改變成分的數量.因此,如果用戶輸入:“我想要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)如何让用户打开文本文件然后更改整数/数字...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 黄色喷水视频 | 情侣黄网站免费看 | 亚洲国产97 | 激情文学欧美 | 亚洲天天综合 | 久久国内视频 | 国产视频在线免费观看 | 美女洗澡无遮挡 | 一边摸一边做爽的视频17国产 | 国产激情图片 | 18性xxxxx性猛交| 国产极品美女在线 | 美女网站视频在线观看 | 国产亚洲精品精品精品 | 爱情岛论坛亚洲品质自拍视频 | 成人国产精品免费观看视频 | 国产欧美日韩综合精品一区 | 国产精品嫩草影院桃色 | 国产精品久久久一区二区 | 一极黄色大片 | 国产美女精品人人做人人爽 | 147人体做爰大胆图片成人 | 超碰网站在线观看 | 影音先锋在线视频观看 | 91超碰在线| 国产精品久久久久久人妻精品动漫 | 春物催眠 | 91天堂在线观看 | 欧美影视一区二区三区 | 欧美日韩国产三区 | 欧美日韩一区二区三区69堂 | 日本动漫艳母 | 一区二区三区www污污污网站 | 狠狠爱婷婷 | 欧美大屁股熟妇bbbbbb | 欧美人与禽zozzo性之恋的特点 | www.日本在线 | 亚洲激情一区二区 | 在线观看中文字幕视频 | 2021亚洲天堂 | 国产在线综合网 | 日本视频不卡 | 免费在线黄网站 | 亚洲不卡在线播放 | 一区二区三区免费视频观看 | 精品国产鲁一鲁一区二区三区 | 先锋影音av中文字幕 | 国产美女一级片 | 嫩草视频在线播放 | 男朋友是消防员第一季 | 国产激情在线视频 | 三女警花合力承欢猎艳都市h | 一区二区精品在线观看 | 午夜成人影视 | 尤物国产在线 | 在线观看av中文字幕 | 亚洲欧美激情一区二区三区 | 思思99精品视频在线观看 | 人人九九精 | 手机看片91 | wwwxxx黄色| www视频在线观看网站 | 爆操巨乳美女 | 视频一区日韩 | 亚洲女人天堂色在线7777 | 亚洲性片 | 91av视频| 天堂精品视频 | 人人妻人人澡人人爽国产一区 | www.色妞 | 亚洲一区二区福利 | 国产 日韩 欧美 成人 | 手机av在线看 | 床戏高潮呻吟声片段 | 国产美女在线精品 | 天天热天天干 | jizz精品| www日本xxx| av福利站 | 午夜国产一区二区三区 | 福利久久久| av自拍网 | 国产这里只有精品 | 欧美另类在线观看 | 国产精品国产一区 | 国产做受高潮 | 久久综合久久综合久久 | 69精品人妻一区二区三区 | 日本在线加勒比 | 老太婆av | 一区二区三区免费看视频 | 亚洲国产精品无码久久久久高潮 | 尤物在线 | 亚洲一级大片 | 天天做天天摸天天爽天天爱 | 狠色综合7777夜色撩人 | 麻豆传媒网站入口 | 日韩av在线看免费观看 | 免费观看成人 |