在for循环外指定变量

时间:2016-01-02 16:28:08

标签: python for-loop

我有以下for循环写入来汇总列表中的所有数字。我知道python中有一个302 40 C:\Program Files\Dev-Cpp\MinGW64\include\graphics.h [Error] redefinition of 'int right' 2 0 C:\Users\user\Documents\solar.cpp In file included from C:\Users\user\Documents\solar.cpp 302 27 C:\Program Files\Dev-Cpp\MinGW64\include\graphics.h [Note] 'int right' previously declared here 28 23 C:\Users\user\Documents\solar.cpp [Error] expected ')' before ',' token 38 16 C:\Users\user\Documents\solar.cpp [Error] expected constructor, destructor, or type conversion before '(' token C:\Users\user\Documents\solar.cpp In member function 'void Planet::Show()': 61 21 C:\Users\user\Documents\solar.cpp [Error] 'random' was not declared in this scope 66 12 C:\Users\user\Documents\solar.cpp [Error] 'newchar' was not declared in this scope C:\Users\user\Documents\solar.cpp In member function 'void Planet::Move()': 88 12 C:\Users\user\Documents\solar.cpp [Error] 'newchar' was not declared in this scope C:\Users\user\Documents\solar.cpp At global scope: 117 13 C:\Users\user\Documents\solar.cpp [Error] '::main' must return 'int' C:\Users\user\Documents\solar.cpp In function 'int main()': 123 38 C:\Users\user\Documents\solar.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] 130 17 C:\Users\user\Documents\solar.cpp [Error] 'BW80' was not declared in this scope 130 21 C:\Users\user\Documents\solar.cpp [Error] 'textmode' was not declared in this scope 131 16 C:\Users\user\Documents\solar.cpp [Error] 'clrscr' was not declared in this scope 133 8 C:\Users\user\Documents\solar.cpp [Error] 'cout' was not declared in this scope 133 8 C:\Users\user\Documents\solar.cpp [Note] suggested alternative: 1 0 C:\Users\user\Documents\solar.cpp In file included from C:\Users\user\Documents\solar.cpp 61 18 C:\Program Files\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\iostream [Note] 'std::cout' 133 67 C:\Users\user\Documents\solar.cpp [Error] 'endl' was not declared in this scope 133 67 C:\Users\user\Documents\solar.cpp [Note] suggested alternative: 39 0 C:\Program Files\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\iostream In file included from C:/Program Files/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/iostream 1 C:\Users\user\Documents\solar.cpp from C:\Users\user\Documents\solar.cpp 564 5 C:\Program Files\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\ostream [Note] 'std::endl' 149 27 C:\Users\user\Documents\solar.cpp [Error] 'random' was not declared in this scope 153 28 C:\Users\user\Documents\solar.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] 154 28 C:\Users\user\Documents\solar.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] 155 28 C:\Users\user\Documents\solar.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] 158 29 C:\Users\user\Documents\solar.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] 159 29 C:\Users\user\Documents\solar.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] 160 29 C:\Users\user\Documents\solar.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] 163 33 C:\Users\user\Documents\solar.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] 164 33 C:\Users\user\Documents\solar.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] 165 33 C:\Users\user\Documents\solar.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] 169 48 C:\Users\user\Documents\solar.cpp [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] 181 34 C:\Users\user\Documents\solar.cpp [Error] no matching function for call to 'Planet::Planet(int, int, int, double)' 181 34 C:\Users\user\Documents\solar.cpp [Note] candidates are: 26 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet() 26 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 0 arguments, 4 provided 10 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet(const Planet&) 10 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 1 argument, 4 provided 182 32 C:\Users\user\Documents\solar.cpp [Error] no matching function for call to 'Planet::Planet(int, int, int, double)' 182 32 C:\Users\user\Documents\solar.cpp [Note] candidates are: 26 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet() 26 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 0 arguments, 4 provided 10 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet(const Planet&) 10 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 1 argument, 4 provided 183 33 C:\Users\user\Documents\solar.cpp [Error] no matching function for call to 'Planet::Planet(int, int, int, double)' 183 33 C:\Users\user\Documents\solar.cpp [Note] candidates are: 26 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet() 26 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 0 arguments, 4 provided 10 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet(const Planet&) 10 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 1 argument, 4 provided 184 33 C:\Users\user\Documents\solar.cpp [Error] no matching function for call to 'Planet::Planet(int, int, int, double)' 184 33 C:\Users\user\Documents\solar.cpp [Note] candidates are: 26 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet() 26 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 0 arguments, 4 provided 10 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet(const Planet&) 10 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 1 argument, 4 provided 185 36 C:\Users\user\Documents\solar.cpp [Error] no matching function for call to 'Planet::Planet(int, int, int, double)' 185 36 C:\Users\user\Documents\solar.cpp [Note] candidates are: 26 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet() 26 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 0 arguments, 4 provided 10 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet(const Planet&) 10 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 1 argument, 4 provided 186 35 C:\Users\user\Documents\solar.cpp [Error] no matching function for call to 'Planet::Planet(int, int, int, double)' 186 35 C:\Users\user\Documents\solar.cpp [Note] candidates are: 26 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet() 26 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 0 arguments, 4 provided 10 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet(const Planet&) 10 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 1 argument, 4 provided 187 35 C:\Users\user\Documents\solar.cpp [Error] no matching function for call to 'Planet::Planet(int, int, int, double)' 187 35 C:\Users\user\Documents\solar.cpp [Note] candidates are: 26 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet() 26 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 0 arguments, 4 provided 10 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet(const Planet&) 10 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 1 argument, 4 provided 188 36 C:\Users\user\Documents\solar.cpp [Error] no matching function for call to 'Planet::Planet(int, int, int, double)' 188 36 C:\Users\user\Documents\solar.cpp [Note] candidates are: 26 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet() 26 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 0 arguments, 4 provided 10 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 1 argument, 4 provided 189 34 C:\Users\user\Documents\solar.cpp [Error] no matching function for call to 'Planet::Planet(int, int, int, double)' 189 34 C:\Users\user\Documents\solar.cpp [Note] candidates are: 26 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet() 26 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 0 arguments, 4 provided 10 8 C:\Users\user\Documents\solar.cpp [Note] Planet::Planet(const Planet&) 10 8 C:\Users\user\Documents\solar.cpp [Note] candidate expects 1 argument, 4 provided 函数。

sum

当我尝试在for循环中初始化tot = 0 var = 5 for num in range(1, var + 1): tot += num print tot 时,它给出了错误的答案:即

tot

有什么理由吗?请注意,我是python的初学者。

1 个答案:

答案 0 :(得分:1)

在循环中,您将tot的值重新分配给零。因此,tot在最后一次迭代中只是var