我正在编写一个代码字游戏(也称为替换字符)程序我正在写的问题,我一直得到错误'str'对象不可调用.这是我的代码(只是涉及这个字典的while循环的一部分:
used_pairings = {}
while.....:
used_pairings[guesschar] = guessletter
print = ("At this point you can decide whether to delete a character-letter pairing.")
used_pairings_choice = input("Type 1 to delete a pairing or ENTER to carry on: ")
if used_pairings_choice == "1":
print ("These are your pairings so far:")
print (used_pairings)
else:
print ("Continuing program.")
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
"Codeword.py", line 79, in <module>
print ("These are your pairings so far:")
TypeError: 'str' object not callable
Run Code Online (Sandbox Code Playgroud)
我完全不同意这意味着什么以及为什么我收到此错误消息所以任何帮助将不胜感激.提前致谢!
我有兴趣设置一个环境来使用 pyOpenGL 编写粒子模拟器。我已经安装了 pyOpenGL。这是测试代码,只是为了确保 opengl 正常工作。
from OpenGL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
window = 0
width, height = 500,400
def draw():
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glutDwapBuffers()
#initialization
glutInit()
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(width,height)
glutInitWindowPosition(0,0)
window = glutCreateWindow(b"noobtute")
glutDisplayFunc(draw)
glutIdleFunc(draw)
glutMainLoop()
Run Code Online (Sandbox Code Playgroud)
但是,当我从命令运行脚本时,出现以下错误:
NameError: name 'glClear' is not defined
GLUT Display callback <function draw at 0x0000000002F8CB70> with (),{} failed:
returning None name 'glClear' is not defined
Run Code Online (Sandbox Code Playgroud)
我尝试重新安装 pyOpengl 但没有成功。
我在 Windows 8.1 上运行 Python …
运行这段代码
d0 = np.ones(N)
dp1 = np.ones(N - 1)
dm1 = np.ones(N - 1)
diag = [[d0],[dp1],[dm1]]
offsets = [0,1,-1]
A = dia_matrix( (diag,offsets), shape=(N,N), dtype=float)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
File "/usr/local/lib/python2.7/dist-packages/scipy/sparse/dia.py", line 109, in __init__
self.data = np.atleast_2d(np.array(arg1[0], dtype=dtype, copy=copy))
Run Code Online (Sandbox Code Playgroud)
ValueError:使用序列设置数组元素。
我不明白我在做什么错!有人可以给我一个正确的榜样来做我想做的事情吗?
我是 django 休息框架的新手。我想知道如果我想实现一个将查询参数作为键值对的 GET api,那么 url 模式会是什么。像这样的东西:
http://example.com/getResource?userid=<userid>&resourceid=<resourceid>
Run Code Online (Sandbox Code Playgroud)
在 django 文档中找不到类似的内容。请指教。
谢谢
我是 python 的初学者,并尝试通过谷歌搜索找到解决方案。但是,我找不到任何我想要的解决方案。
我试图用 python 做的是对数据进行预处理,查找关键字并从大型 csv 文件中获取包含关键字的所有行。
不知何故,嵌套循环经历了just once,然后它就没有经历过second loop。
下面显示的代码是我的代码的一部分,它从文件中查找关键字csv并写入文本文件。
def main():
#Calling file (Directory should be changed)
data_file = 'dataset.json'
#Loading data.json file
with open(data_file, 'r') as fp:
data = json.load(fp)
#Make the list for keys
key_list = list(data.keys())
#print(key_list)
preprocess_txt = open("test_11.txt", "w+", -1, "utf-8")
support_fact = 0
for i, k in enumerate(key_list):
count = 1
#read csv, and split on "," the line
with open("my_csvfile.csv", 'r', encoding = 'utf-8') …Run Code Online (Sandbox Code Playgroud) 我有一个指定了类型参数的函数,T我假设它是一个结构体。然后我将一个迭代器收集T到一个 Vec. 中:
pub fn get_matches<T>(
&self,
) -> Vec< T>
{
...
some_iter.
.map(|(k, _v)| T{key:(**k).to_string(), hits: 0})
.collect()
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
96 | .map(|(k, _v)| T{key:(**k).to_string(), hits: 0})
| ^ not a struct, variant or union type
Run Code Online (Sandbox Code Playgroud)
我试过让返回类型成为类型参数,但我无法弄清楚如何获取Vec的元素类型并实例化它。我只想生成特定形状的元素(即带有key: string, 和hits: usize)并返回调用者期望的任何容器。
我有一个函数将采用另一个函数,如果我作为参数传递的函数为真,则它会执行某些操作,如果为假,则它会执行其他操作,但是如何指定它以便作为参数传递的函数参数是一个返回布尔值的函数?
def a_function(function,value):
if function(value) == True:
do this
else:
do this
Run Code Online (Sandbox Code Playgroud)
但如果“函数”不是返回布尔值的函数,那么我的代码不起作用
我有一个像这样的文本文件:
[0.52, '1_1man::army'], stack
[0.45, '3_3man::army'], flow
[0.52, '1_1man::army'], testing
[0.52, '2_2man:army'], expert
Run Code Online (Sandbox Code Playgroud)
如何加载到文件并打印所有值
'1_1man::army', '3_3man::army', '1_1man::army' and '2_2man:army'
Run Code Online (Sandbox Code Playgroud)
我的代码:
text = open("text.txt", "r").readlines()
print(text[1])
Run Code Online (Sandbox Code Playgroud)
然后实施一些好人分享的解决方案。我无法使用他们的代码,因为我现在拥有的文件与我发布的文件不同(我希望尝试这个新示例)。
我创建了一个typedefformap但不能使用该find方法。这是代码:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
typedef std::map<std::string, int> customType;
customType mod;
mod["something"] = 2;
if (mod.find("something"))
{
std::cout << "found";
}
}
Run Code Online (Sandbox Code Playgroud)
对于上面的代码,我收到如下错误:
main.cpp: In function ‘int main()’:
main.cpp:22:17: error: could not convert ‘mod.std::map<_Key, _Tp, _Compare, _Alloc>::find, int, std::less >, std::allocator, int> > >(std::basic_string(((const char*)"something"), std::allocator()))’ from ‘std::map, int>::iterator {aka std::_Rb_tree_iterator, int> >}’ to ‘bool’
if (mod.find("something"))
~~~~~~~~^~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我知道我可以直接使用std::map而没有,typedef但这段代码只是我想在项目中做的事情的一个例子。请帮助我找到使用该find方法的方法。
我刚刚开始学习C#,现在我正在尝试使用File IO.我在向文件写一个tab(\t)字符时遇到了一些问题.
到目前为止这是我的代码:
static void Main(string[] args)
{
string[] input = Console.ReadLine().Split(' ');
Console.WriteLine(string.Join("\n", input));
File.WriteAllLines(@"C:\Users\Shashank\Desktop\test.txt", input);
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
当我运行脚本并输入此文本时:
hello \twhat \tis \tyour name
Run Code Online (Sandbox Code Playgroud)
以下内容将写入我的文件:
hello
\twhat
\tis
\tyour
name
Run Code Online (Sandbox Code Playgroud)
但是,我希望文件输出看起来像:
hello
what
is
your
name
Run Code Online (Sandbox Code Playgroud)
我已经在线查看但找不到能给我预期结果的解决方案.我也尝试使用StreamWriter但无济于事.
我的功能在下面
def majority(x):
if x > 17:
return True
return False
Run Code Online (Sandbox Code Playgroud)
示例 lambda 伪代码
majority = lambda x: if x > 17 return True ? False
python ×8
python-3.x ×3
c# ×1
c++ ×1
csv ×1
django ×1
eclipse ×1
lambda ×1
nameerror ×1
nested-loops ×1
predicate ×1
pyopengl ×1
python-2.7 ×1
rust ×1
scipy ×1
text ×1
windows-8.1 ×1