這是作業:
- 第一個程式將讀取每個球員的姓名和高爾夫分數作為鍵盤輸入,然后將這些作為記錄保存在名為 golf.txt 的檔案中。(每條記錄都有一個玩家姓名欄位和一個玩家得分欄位。)
- 第二個程式從 golf.txt 檔案中讀取記錄并顯示它們。
到目前為止,這是我的代碼:
def main():
Continue = 'yes'
golf_file = open('golf.txt', 'a')
while Continue == 'yes':
player_name = input("Input the player's name: ")
player_score = input("Input the player's score: ")
golf_file.write(player_name "\n")
golf_file.write(str(player_score) "\n")
Continue = input("Would you like to continue? [yes]: ")
display_file = input("Would you like to display the records from the golf.txt? [yes]: ")
golf_file.close()
if(display_file == 'yes'):
displayfile()
def displayfile():
golf_file = open('golf.txt', 'r')
for line in golf_file:
print("The score of this person is: ",line)
print("The name of this person is: ",line)
golf_file.close()
main()
當名稱/分數列印時,它看起來像這樣:

高爾夫.txt:

uj5u.com熱心網友回復:
您正在閱讀一行(包含名稱),并將其列印為名稱和樂譜。然后你閱讀包含樂譜的下一行)并再次將其列印為名稱和樂譜。
您想交替將一行視為名稱或樂譜,例如
def displayfile():
with open('golf.txt', 'r') as golf_file:
for name in golf_file:
print("The name of this person is: ", name)
score = next(golf_file)
print("The score of this person is: ", score)
因為golf_file是它自己的迭代器,next所以在回圈體中呼叫會使檔案指標前進一行,因此回圈總是通過讀取名稱行來恢復。
更對稱的東西可能是
def displayfile():
with open('golf.txt', 'r') as golf_file:
while True:
try:
name = next(golf_file)
score = next(golf_file)
except StopIteration:
break
print("The name of this person is: ", name)
print("The score of this person is: ", score)
然而,更簡單的方法是將每個名字和分數寫在一行上。
def main():
with open('golf.txt', 'a') as golf_file:
while True:
player_name = input("Input the player's name: ")
player_score = input("Input the player's score: ")
golf_file.write(f"{player_name}\t{player_score}\n")
continue = input("Would you like to continue? [yes]: ")
if continue != 'yes':
break
display_file = input("Would you like to display the records from the golf.txt? [yes]: ")
if display_file == 'yes':
displayfile()
def displayfile():
with open('golf.txt', 'r') as golf_file:
for line in golf_file:
name, score = line.strip().split('\t')
print("The score of this person is: ", name)
print("The name of this person is: ", score)
main()
uj5u.com熱心網友回復:
在一行中,您列印出分數和名稱。例如,第 1 行是sam:
print("The name of this person is: ",line),
所以它列印出來"The name of this person is: sam"。
print("The score of this person is: ",line)
所以它列印出來"The score of this person is: sam"。
我認為你應該添加一個變數來計算像line_number = 1.
如果line_number % 2 == 1(該行應該是名稱)則:
print("The name of this person is: ", line)
如果line_number % 2 == 0(那條線應該是一個分數)那么:
print("The name of this person is: ", line)
在一個回圈之后,該行應該加 1,所以你應該寫:
line_number = 1
畢竟,您的代碼如下所示:
def main():
Continue = 'yes'
golf_file = open('golf.txt', 'a')
while Continue == 'yes':
player_name = input("Input the player's name: ")
player_score = input("Input the player's score: ")
golf_file.write(player_name "\n")
golf_file.write(str(player_score) "\n")
Continue = input("Would you like to continue? [yes]: ")
display_file = input("Would you like to display the records from the golf.txt? [yes]: ")
golf_file.close()
if(display_file == 'yes'):
displayfile()
def displayfile():
golf_file = open('golf.txt', 'r')
line_number = 1
for line in golf_file:
if line_number % 2 == 0:
print("The score of this person is: ",line)
if line_number % 2 == 1:
print("The name of this person is: ",line)
line_number = 1
golf_file.close()
main()
那么問題就解決了!恭喜!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/432396.html
標籤:Python
上一篇:在python中查找字串的平均值
下一篇:強制make使用python2?
