def f(): print(x) x=20+1 f()运行
时间: 2023-08-12 08:05:36 浏览: 47
这段代码会抛出一个 UnboundLocalError 异常,因为在 f 函数中,变量 x 被赋值为 20+1,所以它被视为本地变量。但是,在 print(x) 语句中,x 还没有被赋值,因此它是未绑定的。要解决这个问题,可以在 f 函数中使用 nonlocal 关键字声明 x 是一个外部变量。也可以将 x 作为参数传递给 f 函数,或者将 x 定义为全局变量。
相关问题
# 输出一定范围内的完全数 from math import sqrt def perfect(x, y): A = [] for a in range(x, y + 1): s = 1 for b in range(2, int(sqrt(a) + 1)): if a % b == 0: c = int(a / b) if b > c: break if b == c: s += b else: s += b + c if s == a: A += [a] if 1 in A: A.remove(1) print(A, end=f",共有{len(A)}个完全数\n") def isint(n): f = False if str(n).count(".") == 0 and str(n).count("-") == 0: f = True return f import time as t print("输入两个正整数") i = "0" while i == "0": try: x = int(input(':')) y = int(input(':')) except: print("重新输入!") else: if isint(x) and isint(y): t1 = t.time() perfect(x, y) t2 = t.time() print(f'用时:{round(t2 - t1, 2)}s') else: print("重新输入!") i = input("输入0以重新运行:")给这段代码加个实时变化的计时器
可以在 `perfect` 函数内部添加一个计时器,用于记录计算完全数的时间。具体实现可以在函数开始前记录当前时间,函数执行完毕后再记录一次时间,两次时间的差值即为计算时间。代码如下:
```python
from math import sqrt
import time
def perfect(x, y):
A = []
# 记录开始时间
start_time = time.time()
for a in range(x, y + 1):
s = 1
for b in range(2, int(sqrt(a) + 1)):
if a % b == 0:
c = int(a / b)
if b > c:
break
if b == c:
s += b
else:
s += b + c
if s == a:
A += [a]
if 1 in A:
A.remove(1)
print(A, end=f",共有{len(A)}个完全数\n")
# 记录结束时间
end_time = time.time()
# 计算时间差
elapsed_time = end_time - start_time
print(f'用时:{round(elapsed_time, 2)}s')
def isint(n):
f = False
if str(n).count(".") == 0 and str(n).count("-") == 0:
f = True
return f
print("输入两个正整数")
i = "0"
while i == "0":
try:
x = int(input(':'))
y = int(input(':'))
except:
print("重新输入!")
else:
if isint(x) and isint(y):
perfect(x, y)
else:
print("重新输入!")
i = input("输入0以重新运行:")
```
在 `perfect` 函数中,我们在计算完全数前记录当前时间,计算完毕后再记录一次时间,并计算时间差,最后输出结果。
解一元二次方程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]))
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)