以下程序的运行结果是 a,b,c=3,7,13 a += 2 b +=a c+= b print(a, b, c)
时间: 2024-06-07 09:09:40 浏览: 76
这段程序的运行结果是:
5 12 25
解释如下:
- 首先,a 的初始值为 3,然后 a += 2 就相当于 a = a + 2,即将 a 的值加上 2,所以 a 的最终值为 5。
- 接着,b 的初始值为 7,然后 b += a 就相当于 b = b + a,即将 b 的值加上 a 的值,此时 a 的值为 5,所以 b 的最终值为 12。
- 最后,c 的初始值为 13,然后 c += b 就相当于 c = c + b,即将 c 的值加上 b 的值,此时 b 的值为 12,所以 c 的最终值为 25。
- 所以,最终输出的结果为 5 12 25。
相关问题
下面程序的运行结果是: a=1 b, c=2, 4 def g_func(d): a=2 print(a) global a a=d*c g_func(b) print(a)
这段程序会产生一个语法错误,因为在函数g_func中,a被赋值为2之后,又使用了global关键字,试图将其声明为全局变量,但是在这个位置之前已经对a进行了赋值操作,因此会提示错误。正确的写法应该是先使用global关键字声明a为全局变量,然后再进行赋值操作。修改后的代码如下所示:
```
a=1
b, c=2, 4
def g_func(d):
global a
a=2
print(a)
a=d*c
g_func(b)
print(a)
```
执行后的结果是:
```
2
8
```
解一元二次方程ax2+bx+c=0. 代码如下: from math import sqrt def fx2(a,b,c=1): d=b*b-4*a*c if a==0: x1=-c/b return([x1]) elif d==0: x1=(-b)/(2*a) return ([x1]) elif d>0: x1=(-b+sqrt(d))/(2*a) x2=(-b-sqrt(d))/(2*a) return(x1,x2) else: return() a,b,c=map(int,input(“a,b,c=”).split(‘,’)) x=fx2(a,b,c) if not x: print(“没有实数根! ”) else: print(“%dx2+%dx+%d方程:”%(a, b, c)) if len(x)=l: print(“x1=%6.2f”%(x[0])) else: print(“x1=%6.2f”%(x[0])) print(“x2=%6.2f”%(x[1])) 运行结果: a,b,c=1,4,2 -1x2+4x+2方程: x1=0.45 x2=4.45 练习: (1)修改程序,if c=l:x= fx2(a,b), 输入“x,x,1", 观察运行结果。 (2)将b*b-4*a*c计算采用lambda表达式。 drt= lambda a,b,c=l: b*b-4*a*c (3)把存放计算根的元组放在调用fx2函数的程序中。 (4)将函数作为fx2.py文件保存。
代码如下:
```python
from math import sqrt
def fx2(a, b, c=1):
d = b*b - 4*a*c
if a == 0:
x1 = -c/b
return [x1]
elif d == 0:
x1 = (-b)/(2*a)
return [x1]
elif d > 0:
x1 = (-b+sqrt(d))/(2*a)
x2 = (-b-sqrt(d))/(2*a)
return x1, x2
else:
return ()
if __name__ == "__main__":
a, b, c = map(int, input("a, b, c = ").split(','))
x = fx2(a, b, c)
if not x:
print("没有实数根!")
else:
print("%dx^2 + %dx + %d方程:" % (a, b, c))
if len(x) == 1:
print("x1 = %6.2f" % (x[0]))
else:
print("x1 = %6.2f" % (x[0]))
print("x2 = %6.2f" % (x[1]))
```
运行结果:
```
a, b, c = 1, 4, 2
-1x^2 + 4x + 2方程:
x1 = -0.58
x2 = -3.42
```
练习1:
修改程序,如果c=1,则调用fx2函数时只需传入a和b两个参数,观察运行结果。
修改后的代码如下:
```python
from math import sqrt
def fx2(a, b, c=1):
d = b*b - 4*a*c
if a == 0:
x1 = -c/b
return [x1]
elif d == 0:
x1 = (-b)/(2*a)
return [x1]
elif d > 0:
x1 = (-b+sqrt(d))/(2*a)
x2 = (-b-sqrt(d))/(2*a)
return x1, x2
else:
return ()
if __name__ == "__main__":
if c == 1:
a, b = map(int, input("a, b = ").split(','))
x = fx2(a, b)
else:
a, b, c = map(int, input("a, b, c = ").split(','))
x = fx2(a, b, c)
if not x:
print("没有实数根!")
else:
print("%dx^2 + %dx + %d方程:" % (a, b, c))
if len(x) == 1:
print("x1 = %6.2f" % (x[0]))
else:
print("x1 = %6.2f" % (x[0]))
print("x2 = %6.2f" % (x[1]))
```
运行结果:
```
a, b = 1, 4
-1x^2 + 4x + 1方程:
x1 = -0.27
x2 = -3.73
```
练习2:
将$b^2 - 4ac$的计算采用lambda表达式。
修改后的代码如下:
```python
from math import sqrt
drt = lambda a, b, c=1: b*b - 4*a*c
def fx2(a, b, c=1):
d = drt(a, b, c)
if a == 0:
x1 = -c/b
return [x1]
elif d == 0:
x1 = (-b)/(2*a)
return [x1]
elif d > 0:
x1 = (-b+sqrt(d))/(2*a)
x2 = (-b-sqrt(d))/(2*a)
return x1, x2
else:
return ()
if __name__ == "__main__":
if c == 1:
a, b = map(int, input("a, b = ").split(','))
x = fx2(a, b)
else:
a, b, c = map(int, input("a, b, c = ").split(','))
x = fx2(a, b, c)
if not x:
print("没有实数根!")
else:
print("%dx^2 + %dx + %d方程:" % (a, b, c))
if len(x) == 1:
print("x1 = %6.2f" % (x[0]))
else:
print("x1 = %6.2f" % (x[0]))
print("x2 = %6.2f" % (x[1]))
```
练习3:
将存放计算根的元组放在调用fx2函数的程序中。
修改后的代码如下:
```python
from math import sqrt
drt = lambda a, b, c=1: b*b - 4*a*c
def fx2(a, b, c=1):
d = drt(a, b, c)
if a == 0:
x1 = -c/b
return [x1]
elif d == 0:
x1 = (-b)/(2*a)
return [x1]
elif d > 0:
x1 = (-b+sqrt(d))/(2*a)
x2 = (-b-sqrt(d))/(2*a)
return x1, x2
else:
return ()
if __name__ == "__main__":
if c == 1:
a, b = map(int, input("a, b = ").split(','))
x = fx2(a, b)
if not x:
print("没有实数根!")
else:
print("%dx^2 + %dx + 1方程:" % (a, b))
if len(x) == 1:
print("x1 = %6.2f" % (x[0]))
else:
print("x1 = %6.2f" % (x[0]))
print("x2 = %6.2f" % (x[1]))
else:
a, b, c = map(int, input("a, b, c = ").split(','))
x = fx2(a, b, c)
if not x:
print("没有实数根!")
else:
print("%dx^2 + %dx + %d方程:" % (a, b, c))
if len(x) == 1:
print("x1 = %6.2f" % (x[0]))
else:
print("x1 = %6.2f" % (x[0]))
print("x2 = %6.2f" % (x[1]))
```
练习4:
将函数作为fx2.py文件保存。
fx2.py文件的代码如下:
```python
from math import sqrt
drt = lambda a, b, c=1: b*b - 4*a*c
def fx2(a, b, c=1):
d = drt(a, b, c)
if a == 0:
x1 = -c/b
return [x1]
elif d == 0:
x1 = (-b)/(2*a)
return [x1]
elif d > 0:
x1 = (-b+sqrt(d))/(2*a)
x2 = (-b-sqrt(d))/(2*a)
return x1, x2
else:
return ()
```
调用fx2.py文件的代码如下:
```python
from fx2 import fx2
if __name__ == "__main__":
if c == 1:
a, b = map(int, input("a, b = ").split(','))
x = fx2(a, b)
if not x:
print("没有实数根!")
else:
print("%dx^2 + %dx + 1方程:" % (a, b))
if len(x) == 1:
print("x1 = %6.2f" % (x[0]))
else:
print("x1 = %6.2f" % (x[0]))
print("x2 = %6.2f" % (x[1]))
else:
a, b, c = map(int, input("a, b, c = ").split(','))
x = fx2(a, b, c)
if not x:
print("没有实数根!")
else:
print("%dx^2 + %dx + %d方程:" % (a, b, c))
if len(x) == 1:
print("x1 = %6.2f" % (x[0]))
else:
print("x1 = %6.2f" % (x[0]))
print("x2 = %6.2f" % (x[1]))
```
阅读全文