首页 > 解决方案 > Python Program counting and list question

问题描述

I made a Python program to repeat the number of internships that I applied for, BUT I need it to store the amount I add so that I can ask it later how many I applied for, rather than me having to enter a number every time. Also, I want it to be able to update if the number of internships I applied for changes, I will input the new number of internships manually. How can I change my program to do that? Please take a look thanks

    print("Welcome back, Ryan")
internships = {} 
asking_ryan = True

amount = input("Enter how many internships you have applied for: ")
amount = int(amount)


if amount > 1:
    print("You have applied for: " + str(amount) + " internship(s)")
    str(amount)

if amount < 1:
    print("Error! you cannot apply for 0 internships!")


if amount == 1:
    print("You have applied for: " + str(amount) + " internship") 

Program output: Welcome back, Ryan.

Enter how many internships you have applied for: 2

You have applied for: 2 internship(s)

标签: python

解决方案


我看到了你的问题。我有点理解。

您希望能够更新您已完成的实习,这样您就不必总是重新运行该程序以便将您的实习计入您的实习?

您可能必须为此使用一些文件,应该很简单!

首先,您可以创建一个名为opens.txt的文件,并在该文件中添加一个 0,以跟踪您打开该程序的次数。当您运行程序时,请执行以下操作:

opens = open("opens.txt", w+)
open_count = int(opens.read())
opens.write(open_count + 1)

if open_count == 1:
    amount = input("Enter how many internships you have applied for: ")
    ... # Other Code

然后创建一个名为的文件internships.txt,该文件将存储您当前拥有的实习次数,默认为 0。

internships = open("internships.txt", w+)
internship_count = int(internships.read())

print("You currently have applied to {} internships since last time...".format(internship_count)

new_internships = input("How many new internships have you applied for?  ")

internships.write(internship_count + new_internships)

我认为这应该有帮助吗?好久没用文件了。下次请更好地表达你的问题。


推荐阅读