我刚刚开始使用C++ Primer Plus学习C++,但我遇到了其中一个例子的问题.就像我指示的那本书一样,我cin.get()在最后包括了防止控制台自行关闭.但是,在这种情况下,它仍然自行关闭,除非我添加两个cin.get()我不理解的语句.我正在使用Visual Studio Express 2010.
#include <iostream>
int main()
{
int carrots;
using namespace std;
cout << "How many carrots do you have?" << endl;
cin >> carrots;
carrots = carrots + 2;
cout << "Here are two more. Now you have " << carrots << " carrots.";
cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我是Python新手,我一直在读到使用global将变量传递给其他函数被认为是菜鸟,也是一种不好的做法。我想不再使用全局变量,但我不知道该怎么做。
现在,我有一个在 wxPython 中创建的 UI 作为其自己的单独类,并且我有另一个从 .ini 文件加载设置的类。由于 UI 中的设置应与 .ini 中的设置相匹配,因此如何传递这些值?我可以使用类似:Settings = Settings()然后将变量定义为类似的东西self.settings1,但随后我必须创建Settings一个全局变量以将其传递给我的 UI 类(如果我在其中分配,则不会是这样main())。
那么传递这些变量的正确且Python式的方法是什么?
编辑:这是我正在使用的代码,我试图让它像 Alex Martelli 的示例一样工作。以下代码保存在Settings.py:
import ConfigParser
class _Settings():
@property
def enableautodownload(self): return self._enableautodownload
def __init__(self):
self.config = ConfigParser.ConfigParser()
self.config.readfp(open('settings.ini'))
self._enableautodownload=self.config.getboolean('DLSettings', 'enableautodownload')
settings = _Settings()
Run Code Online (Sandbox Code Playgroud)
Settings.settings.enableautodownload每当我尝试从另一个文件引用时,我都会得到: AttributeError: 'module' object has no attribute 'settings'. 我究竟做错了什么?
编辑2:别介意这个问题,我重新输入了代码,现在它可以工作了,所以它一定是一个简单的拼写或语法错误。
我正在尝试使用pyOpenGL,但我无法让相机正常工作.
def Draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glRotatef(roty,1,0,0)
glRotatef(rotx,0,1,0)
glTranslate(0,0,eyez)
glPushMatrix()
glTranslate(0,0,-80)
glBegin(GL_QUADS)
glColor(0,1,0)
glVertex3f(-50,-5,10)
glVertex3f(-50,50,10)
glVertex3f(50,50,10)
glVertex3f(50,-5,10)
glColor(1,0,0)
glVertex3f(-50,-5,10)
glVertex3f(-50,50,10)
glVertex3f(-50,50,70)
glVertex3f(-50,-5,70)
glEnd()
glPopMatrix()
glutSwapBuffers()
Run Code Online (Sandbox Code Playgroud)
这些旋转效果很好,但我无法向相机朝向的方向前进.当我修改代码以便能够以这种方式向前移动时,场景将无法正确旋转.如果我回到足够远的地方,将会发生一个圆圈旋转的情况.