基于事件评分的用户选择的Python输入循环

基于事件评分的用户选择的Python输入循环,python,Python,该方案的设想如下: 1) 要求用户选择1或2 2) 如果选择了一个,则提示用户输入分数。如果选择2,则用户应能够查看分数 3) 用户输入或查看分数后,应能够添加其他分数或查看分数。键入Y将使它们返回到程序的开头N应该说他们可以关闭程序,或者关闭它 目前,数字选择适用于分数输入,但我不确定如何使程序在用户希望添加或查看更多分数时重新启动 这是当前状态下的程序 print ("Type 1 to a add a score.") print ("Type 2 to view scores.") ac

该方案的设想如下:

1) 要求用户选择1或2

2) 如果选择了一个,则提示用户输入分数。如果选择2,则用户应能够查看分数

3) 用户输入或查看分数后,应能够添加其他分数或查看分数。键入
Y
将使它们返回到程序的开头
N
应该说他们可以关闭程序,或者关闭它

目前,数字选择适用于分数输入,但我不确定如何使程序在用户希望添加或查看更多分数时重新启动

这是当前状态下的程序

print ("Type 1 to a add a score.")
print ("Type 2 to view scores.")
action = input("Please type a number: ")
if action == "1":
    print ("Enter a score?")
    eventscore = int(input ("Please type their score: ") 
    score = eventscore
    f = open("scores.txt", "a")
    f.write(eventscore)
    f.write("\n")
    f.close()
    print("The score for" , score, "has been saved.")
elif action == "2":
    print ("Type 1 to view all scores.")
    print ("Type 2 to view scores for a specific team.")
    scorecheck = input("Please type a number: ")
    if scorecheck == "1":
        f = open("scores.txt", "r")
        for line in f:
            allscores = f.readlines()
        print(allscores)
    f.close
elif scorecheck == "2": 
        teamcheck= input ("Please enter the team name: ")

while True:
    while True:
        answer = input('Want to add a new score or view existing scores? (Y/N): ')
        if answer in ('Y', 'N'):
            break
        print ("Please enter 'Y' or 'N'.")
    if answer == 'y':
        continue
    else:
        print ("You can now close the program.")
        break
当前程序可以启动,请用户选择是否要添加或查看分数,然后询问是否要添加/查看更多或关闭程序。如果用户输入“Y”,程序应完全重新启动,但仍需要添加/查看循环。如果他们键入
N
,程序应关闭

任何帮助都将不胜感激,因为我不知道如何在整个程序中获得循环,因为它已经包含多个循环

while True:
    ans = input("Enter option (y, Y):")
    if ans == "":
        print("finished - exit program")
        break
    if ans in ['y', 'Y']:
        askQuestion()

def askQuestion():
    ...top bit of your code
在我的手机上,所以只有peudocode提供。还有比这更好的办法。正如国际象棋中所说的,如果你找到了一个好的招式,就找一个更好的。 使用上下文管理器在
中打开文件也是一种规范,如下所示:

with open('filename.txt', 'a') as f:
    f.write..etc

平面比嵌套好…这里不需要两个while循环。你能提供一个答案吗?因为我试过了,但没有用,谢谢@AndrewAllenThank,我会试试的