【问题标题】:how can I access the variable inside a function to other functions?如何将函数内的变量访问到其他函数?
【发布时间】:2022-06-14 20:47:44
【问题描述】:

请帮助我如何访问位于函数中的变量 lastPattern 并将其访问到其他函数。这是我的代码:

def easyLevel():
global score
n = 2
while n <= 6:
    pattern = random.choice(string.ascii_lowercase)
    for i in range(n-1): 
        pattern = pattern + " " + random.choice(string.ascii_lowercase)
        
    print("\nThe pattern is: ")
    print(pattern + "\n")
    countDown(5)

    easyAns = str(input("What was the pattern?: "))

    if easyAns == pattern:
        n = n + 1
        print("That's correct!")
        score = score + 5
        allPatterns.append(pattern)
    else:
        print("\nSorry, that's incorrect.")
        print("Your score is: " + str(score))
        break

lastPattern = allPatterns[-1]
with open('Leaderboard.csv', 'a') as file:
    csv_writer = csv.writer(file)
    csv_writer.writerow([name, score, lastPattern])

【问题讨论】:

  • 我认为您的代码格式不正确。请仔细检查。

标签: python function


猜你喜欢
  • 1970-01-01
  • 2018-03-07
  • 2017-12-02
  • 2013-09-13
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多