本人刚开始学习Python,求指点
为什么我一运行下面的代码就报错?
NameError: name 'h' is not defined
import math
def fileas():
if h==1:
aa()
elif h==2:
ab()
elif h==3:
ac()
else:
print('滚!')
def aa():
a=float(a)
def ab():
a=math.ceil(a)
def ac():
a=math.floor(a)
def fileb():
b=int(input('请输入总数:'))
c=int(input('请输入:'))
print('请输入要处理的类型(四舍五入 1 向上取整 2 向下取整 3)')
h=int(input('请输入:'))
a=b/c
fileb()
fileas()
fileb里的h和fileas里的h都是局部变量,互相无法直接调用,可以使用global关键字把变量变成全局变量。后面的a也是同样的问题
除了变量问题,函数定义也有问题,只能尽力这么改了,而且结果并没有打印出来:
import math
def fileas():
def aa(a):
a=float(a)
def ab(a):
a=math.ceil(a)
def ac(a):
a=math.floor(a)
if h==1:
aa(a)
elif h==2:
ab(a)
elif h==3:
ac(a)
else:
print('滚!')
def fileb():
global h
global a
b=int(input('请输入总数:'))
c=int(input('请输入:'))
print('请输入要处理的类型(四舍五入 1 向上取整 2 向下取整 3)')
h=int(input('请输入:'))
a=b/c
fileb()
fileas()