我正在尝试学习python,并且很遗憾地看到这段代码我无法弄清楚为什么结果达到33 !!!
调试是什么?有人告诉我,帮助你找到答案,但我不知道怎么做才能找到答案。
这是我目前的代码
x = "c"
y = 3
if "x" in "computer science":
y = y + 5
else:
y = y + 10
if x in "computer science":
y = y + 20
else:
y = y + 40
print (y)
答案 0 :(得分:0)
在第3行中,您输入了"x"
而不是x
,这意味着您正在“计算机科学”中寻找x
字符串,而不是那里,所以代码将10添加到y
而不是5
答案 1 :(得分:0)
容易腻
第一个条件'driver' => 'apc',
'models' => [
\App\Apc\User\User::MODULE_NAME => \App\Apc\User\User::class,
\App\Apc\Company\Company::MODULE_NAME => \App\Apc\Company\Company::class
],
正在字符串if "x" in "computer science":
中查找字符串"x"
。请注意x周围的引号。这意味着它是一个字符串文字,而不是变量x。
这意味着执行"computer science"
代码块,将else
添加到10
的值(此前为3)。
y
的价值现在为y
然后,第二个条件int(13)
正在查找字符串文字if x in "computer science"
中变量x
的值。由于变量"computer science"
的值是字符串文字x
,因此有三个" c""计算机科学":条件计算结果为True,并执行第一个代码块。
第一个代码块将"c"
的值增加20,即y
。因此,int(13)
最终成为y
(33)
答案 2 :(得分:0)
CapsuleCompany=BR
初始化为y
。第一个3
语句会检查字符if
是否在字符串x
中,但不是,因此我们将computer science
添加到10
。 y
现在等于13.在第二个y
语句中检查变量if
的值是否为x
,这是否在字符串c
中,我们将computer science
添加到20
。
y
为y
。
答案 3 :(得分:0)
这是你的第一个if语句中的原因
x = "c" #x is a variable containing a string 'c'
y = 3
if "x" in "computer science":
# in this if statement the condition checked if the string x is in "computer science" it didn't check the variable x, just a string 'x' so this statement below will not be executed since the condition is not satisfied
y = y + 5
else: # but this one will be executed since it is the else statement and the if statement is not satisfied which will overwrite the value of y from 3 to 13
y = y + 10
so now the value of y=13
#in your second if statement
if x in "computer science":
#you checked if the variable x is in computer science which basically is true because its is the same as asking 'is 'c' in computer science' so the statement mellow will be executed which will overwrite the value of y from 13 to 33 so when you print y you will get 3
y = y + 20
else:
y = y + 40
答案 4 :(得分:0)
- >如果“计算机科学”中的“x”:这意味着如果子串“x”(这里它是双引号,所以它将被视为一个字符串)在字符串“计算机科学”中然后执行if块代码。由于“x”不在“计算机科学”中,因此将执行以下其他块
else:
y = y + 10
因此在此之后y = 13
- >现在,另一个if块将检查其条件
if x in "computer science":
y = y + 20
else:
y = y + 40
这里,在这段代码中,x被视为存储“c”的变量,因为在“计算机科学”中有“c”(在第一个字符处),if块代码将被执行更新值y = 13至y = 13 + 20 = 33
- >简单来说,调试意味着识别和删除代码中的错误。 有关调试https://en.wikipedia.org/wiki/Debugging
的更多信息,请参阅此链接答案 5 :(得分:0)
你已经得到了详细的解释(只需要你的大脑)。 wrt /调试工具等,至少有两个可以帮助你跟踪代码执行(这是重点)。
第一个是简单的,它被命名为" print"。您所要做的就是在代码中添加一些print()
以找出执行哪些部分(以及从哪些部分中扣除)以及变量在这些点上的值:
x = "c"
y = 3
print('start : x = "{}", y = {}'.format(x, y))
if "x" in "computer science":
y = y + 5
print('"x" in "computer science" - now y = {}'.format(y))
else:
y = y + 10
print('"x" not in "computer science" - now y = {}'.format(y))
if x in "computer science":
y = y + 20
print('x ( = "{}") in "computer science" - now y = {}'.format(x, y))
else:
y = y + 40
print('x ( = "{}") not in "computer science" - now y = {}'.format(x, y))
print (y)
执行时将显示:
start : x = "c", y = 3
"x" not in "computer science" - now y = 13
x ( = "c") in "computer science" - now y = 33
33
另一个工具被命名为步调试器,并且有点复杂(但功能更强大)。 Python comes with a (command line) step debugger included,它可能不是"性感"作为GUI之一,但需要零安装,零配置,并保证在所有环境中工作。 (我不会在这里解释如何使用它,因为它已经记录在案了......)
答案 6 :(得分:-1)
if "x" in "computer science":
if x in "computer science":
这是答案。 x与" x"
不同