![](/img/trans.png)
[英]Python: Simple Question regarding counting characters on a list of strings
[英]Python Program counting and list question
我做了一個 Python 程序來重復我申請的實習次數,但我需要它來存儲我添加的數量,以便我以后可以詢問我申請了多少,而不是我每次都必須輸入一個數字。 另外,如果我申請的實習數量發生變化,我希望它能夠更新,我將手動輸入新的實習數量。 我怎樣才能改變我的程序來做到這一點? 請看一下謝謝
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")
程序 output:歡迎回來,瑞恩。
輸入您申請的實習次數:2
您已申請:2 份實習
我看到了你的問題。 我有點理解。
您希望能夠更新您已完成的實習,這樣您就不必總是重新運行該程序以便將您的實習計入您的實習?
您可能必須為此使用一些文件,應該很簡單!
首先,您可以創建一個名為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)
我認為這應該有幫助嗎? 好久沒用文件了。 下次請更好地表達你的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.