我搜索了一段时间,但找不到答案,所以我希望它不是重复的。
我有以下代码:
this.Controls.Add(new Label { Location = new Point(10, 10),
AutoSize = true,
Name = "jobNumStatic",
Text = "Job Number:",
Font = new Font(jobNumStatic.Font, FontStyle.Bold) });
Run Code Online (Sandbox Code Playgroud)
我正在尝试将字体更改为粗体。但是该代码给出了错误,当前上下文中不存在名称“jobNumStatic”。有没有办法让字体在这里加粗?
我也试过:
jobNumStatic.Font = new Font(jobNumStatic.Font, FontStyle.Bold) });
Run Code Online (Sandbox Code Playgroud)
声明标签后,它给了我同样的错误。
我正在尝试在python中编写一个"switch"字典.我希望能够从文本文件中读取数据并根据其数据类型执行不同的操作.因此,例如,如果我读入一个字符串,我想将它与另一个字符串进行比较.或者,如果我在浮点数中读取,我想用它做一些操作.这是机器学习程序的数据清理操作.
我可以使用If ... Else语句来做到这一点,但是因为我可以想象每种数据类型都有一些东西,所以我宁愿做得更干净.
我正在使用以下代码:
varX = 2.0
switchDict = {"bool": "boolean", "int": "integer","float": "floatType",
"str": "string"}
switchDict[str(type(varX))]()
def boolean():
print("You have a boolean" )
def integer():
print("You have an integer")
def floatType():
print("You have a float")
def string():
print("You have a string”)
Run Code Online (Sandbox Code Playgroud)
它返回:
Traceback (most recent call last):
File "/Gower71/Switch.py", line 5, in <module>
switchDict[str(type(varX))]()
KeyError: "<class ‘float'>"
Run Code Online (Sandbox Code Playgroud)
如果我将switchDict行更改为:
switchDict = {bool: "boolean", int: "integer", float: "floatType", str: "string"}
switchDict[type(varX)]()
Run Code Online (Sandbox Code Playgroud)
它返回:
Traceback (most recent call last):
File "/Gower71/Switch.py", line …Run Code Online (Sandbox Code Playgroud) 我正在写一本关于Python3和线性代数的书.我正在尝试使用格式'name junk junk 1 1 1 1 1'的字符串,并创建一个字典,其中包含名称和从字符串转换为整数的数字.即{name:[1,1,1,1,1]}但我无法弄清楚循环,因为我是一个蟒蛇新手.这是我的代码:
string = 'Name junk junk -1 -1 1 1'
for i, x in string.split(" "):
if i == 0:
a = x
if i > 2:
b = int(x)
Run Code Online (Sandbox Code Playgroud)
运行该代码会发出以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
Run Code Online (Sandbox Code Playgroud)
理想情况下,我也希望它是一种理解.但是,如果我能得到循环,我可能会想出那个部分.