plapapyjh 2022-09-04 13:15 采纳率: 43.5%
浏览 39
已结题

Python初学者,在将代码添加用文件打开的功能时卡住了

Python初学者,在将代码添加用文件打开的功能时卡住了

"""
  Update your program so that it prints (writes) the output to a file.
How do we do this?
• First you need to open the file for writing. You only need to do this once, so add this line before
your loop starts:
out_file = open(OUTPUT_FILE, 'w')
Note that this code line expects you to define the constant OUTPUT_FILE, so do that above.
• Update any print statements, so they output to the file.
Here’s an (incomplete) example:
print("${:,.2f}".format(price), file=out_file)
• Close the file at the very end:
out_file.close()
• This version uses the .format() method when printing the price. Change this to use f-string
formatting (keet the same output).
"""
import random

MAX_INCREASE = 0.1  # 10%
MAX_DECREASE = 0.05  # 5%
MIN_PRICE = 0.01
MAX_PRICE = 1000.0
INITIAL_PRICE = 10.0
day = 1
print("On day {} price is:".format(day),end="")

price = INITIAL_PRICE
print("${:,.2f}".format(price))

while price >= MIN_PRICE and price <= MAX_PRICE:
    price_change = 0
    # generate a random integer of 1 or 2
    # if it's 1, the price increases, otherwise it decreases
    if random.randint(1, 2) == 1:
        # generate a random floating-point number
        # between 0 and MAX_INCREASE
        price_change = random.uniform(0, MAX_INCREASE)
        day+=1
        print("On day {} price is:".format(day), end="")
    else:
        # generate a random floating-point number
        # between negative MAX_DECREASE and 0
        price_change = random.uniform(-MAX_DECREASE, 0)
        day+=1
        print("On day {} price is:".format(day), end="")

    price *= (1 + price_change)
    print("${:,.2f}".format(price))

上面代码顶部是给的实现用文件打开功能要求,在这部分我卡住了,麻烦看一下,给一个修改后的代码,如果有注释就更好啦,谢谢

  • 写回答

1条回答 默认 最新

  • 关注

    请参考,运行后会生成文件tmp.txt,内容如下:

    img

    
    import random
    
    OUTPUT_FILE = 'tmp.txt'  # 定义文件名,可根据需求更改,这里定义文件名为 tmp.txt
    out_file = open(OUTPUT_FILE, 'w')  # 打开文件
    
    MAX_INCREASE = 0.1  # 10%
    MAX_DECREASE = 0.05  # 5%
    MIN_PRICE = 0.01
    MAX_PRICE = 1000.0
    INITIAL_PRICE = 10.0
    day = 1
    
    # f-string格式化打印内容参考:https://blog.csdn.net/wuli_xin/article/details/117979698
    # print()函数中增加 file = out_file表示把该行语句输出到文件中,下面的print语句中均加入file = out_file
    print(f"On day {day} price is:", end="",file=out_file)
    price = INITIAL_PRICE
    
    print(f"${price:.2f}",file=out_file)
    
    while price >= MIN_PRICE and price <= MAX_PRICE:
        price_change = 0
        # generate a random integer of 1 or 2
        # if it's 1, the price increases, otherwise it decreases
        if random.randint(1, 2) == 1:
            # generate a random floating-point number
            # between 0 and MAX_INCREASE
            price_change = random.uniform(0, MAX_INCREASE)
            day += 1
            print(f"On day {day} price is:", end="",file=out_file)
        else:
            # generate a random floating-point number
            # between negative MAX_DECREASE and 0
            price_change = random.uniform(-MAX_DECREASE, 0)
            day += 1
            print(f"On day {day} price is:", end="",file=out_file)
        price *= (1 + price_change)
        print(f"${price:.2f}",file=out_file)
    
    out_file.close()  # 关闭文件
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月12日
  • 已采纳回答 9月4日
  • 赞助了问题酬金10元 9月4日
  • 创建了问题 9月4日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改