定义全局变量时出错

时间:2021-04-08 14:42:02

标签: python python-3.x syntax

我在声明全局变量时遇到错误。我在定义中定义了一个全局变量。

import os
import time as t
s = t.time()
import numpy as np
#print(os.getcwd())
#print(os.listdir(os.getcwd()))
chDir = os.chdir("/storage/emulated/0/OS-TEST-FOLDER")
print(chDir)
#print(dir(os))
file = open("Times.txt",'w')
tann = []


def tanns():
    while True:
        epochs = int(input("Enter how many epochs please >>> "))
        
        
        for i in range(epochs):
            aa = np.tan(i)
            print(f'Writing Tan{i} in Process')
            aaa = f'tan {i} degree = {aa}'
        #   t.sleep(0.01)
            tann.append(aaa)
        #with open(file) as f:
        file.write(str(tann))
        #print(file)
            
        f = t.time()
        dur = f - s
        print(f'Finished writing {epochs} tan')
        print(f"The Program Finished in {dur} seconds ")
        
        global choice = str(input("Do you want to continue running the Program >>> "))
        
        if choice == "n":
            break
        
if choice == 'y':
    tanns()

这是我运行这段代码时遇到的错误。

Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
    start(fakepyfile,mainpyfile)
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
    exec(open(mainpyfile).read(),  __main__.__dict__)
  File "<string>", line 34
    global choice = str(input("Do you want to continue running the Program >>> "))
                  ^
SyntaxError: invalid syntax

[Program finished]

1 个答案:

答案 0 :(得分:1)

必须先声明全局变量,然后才能为其赋值。

global c = ...改为

global c
c = ...
相关问题