python - 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)
解决方案
我看到了你的问题。我有点理解。
您希望能够更新您已完成的实习,这样您就不必总是重新运行该程序以便将您的实习计入您的实习?
您可能必须为此使用一些文件,应该很简单!
首先,您可以创建一个名为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)
我认为这应该有帮助吗?好久没用文件了。下次请更好地表达你的问题。
推荐阅读
- flutter - Flutter 如何将参数传递给“get”方法?
- r - 根据季度观察计算年化回报
- python - 将项目附加到来自 xml 的 dict 列表
- jquery - 如何使用 jquery 和 while 循环动态分配 id
- javascript - 从嵌套数组中删除无子元素(叶子除外)
- wordpress - 如何删除 wordpress 页面中的上次更新日期?
- jenkins - 无法从浏览器连接到 jenkins
- java - 多对多的 JPA 标准规范
- javascript - 如何在 Google TimeLine 图表中仅在一行中设置一个栏(带有分组的行标签)?
- python - 在运行时动态加载客户端库的大多数 Pythonic/Django 方式